Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2014 Linaro Ltd. |
| 4 | * Author: Ashwin Chaugule <ashwin.chaugule@linaro.org> |
| 5 | * |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 6 | * PCC (Platform Communication Channel) is defined in the ACPI 5.0+ |
| 7 | * specification. It is a mailbox like mechanism to allow clients |
| 8 | * such as CPPC (Collaborative Processor Performance Control), RAS |
| 9 | * (Reliability, Availability and Serviceability) and MPST (Memory |
| 10 | * Node Power State Table) to talk to the platform (e.g. BMC) through |
| 11 | * shared memory regions as defined in the PCC table entries. The PCC |
| 12 | * specification supports a Doorbell mechanism for the PCC clients |
| 13 | * to notify the platform about new data. This Doorbell information |
Ashwin Chaugule | 33350e6 | 2015-01-27 16:03:57 -0500 | [diff] [blame] | 14 | * is also specified in each PCC table entry. |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 15 | * |
Ashwin Chaugule | 33350e6 | 2015-01-27 16:03:57 -0500 | [diff] [blame] | 16 | * Typical high level flow of operation is: |
| 17 | * |
| 18 | * PCC Reads: |
| 19 | * * Client tries to acquire a channel lock. |
| 20 | * * After it is acquired it writes READ cmd in communication region cmd |
| 21 | * address. |
| 22 | * * Client issues mbox_send_message() which rings the PCC doorbell |
| 23 | * for its PCC channel. |
| 24 | * * If command completes, then client has control over channel and |
| 25 | * it can proceed with its reads. |
| 26 | * * Client releases lock. |
| 27 | * |
| 28 | * PCC Writes: |
| 29 | * * Client tries to acquire channel lock. |
| 30 | * * Client writes to its communication region after it acquires a |
| 31 | * channel lock. |
| 32 | * * Client writes WRITE cmd in communication region cmd address. |
| 33 | * * Client issues mbox_send_message() which rings the PCC doorbell |
| 34 | * for its PCC channel. |
Tom Saeger | 9d2e8b9 | 2021-03-12 19:31:10 -0700 | [diff] [blame] | 35 | * * If command completes, then writes have succeeded and it can release |
Ashwin Chaugule | 33350e6 | 2015-01-27 16:03:57 -0500 | [diff] [blame] | 36 | * the channel lock. |
| 37 | * |
| 38 | * There is a Nominal latency defined for each channel which indicates |
| 39 | * how long to wait until a command completes. If command is not complete |
| 40 | * the client needs to retry or assume failure. |
| 41 | * |
| 42 | * For more details about PCC, please see the ACPI specification from |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 43 | * http://www.uefi.org/ACPIv5.1 Section 14. |
| 44 | * |
| 45 | * This file implements PCC as a Mailbox controller and allows for PCC |
| 46 | * clients to be implemented as its Mailbox Client Channels. |
| 47 | */ |
| 48 | |
| 49 | #include <linux/acpi.h> |
| 50 | #include <linux/delay.h> |
| 51 | #include <linux/io.h> |
| 52 | #include <linux/init.h> |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 53 | #include <linux/interrupt.h> |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 54 | #include <linux/list.h> |
| 55 | #include <linux/platform_device.h> |
| 56 | #include <linux/mailbox_controller.h> |
| 57 | #include <linux/mailbox_client.h> |
Prakash, Prashanth | 8b0f578 | 2016-02-17 13:21:01 -0700 | [diff] [blame] | 58 | #include <linux/io-64-nonatomic-lo-hi.h> |
Hoan Tran | 6ca595a | 2016-11-14 11:19:02 -0800 | [diff] [blame] | 59 | #include <acpi/pcc.h> |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 60 | |
| 61 | #include "mailbox.h" |
| 62 | |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 63 | #define MBOX_IRQ_NAME "pcc-mbox" |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 64 | |
| 65 | static struct mbox_chan *pcc_mbox_channels; |
| 66 | |
Sudeep Holla | 80b2bdd | 2021-09-17 14:33:46 +0100 | [diff] [blame] | 67 | /** |
| 68 | * struct pcc_chan_info - PCC channel specific information |
| 69 | * |
Sudeep Holla | 0f2591e | 2021-09-17 14:33:49 +0100 | [diff] [blame] | 70 | * @chan: PCC channel information with Shared Memory Region info |
Sudeep Holla | 80b2bdd | 2021-09-17 14:33:46 +0100 | [diff] [blame] | 71 | * @db_vaddr: cached virtual address for doorbell register |
| 72 | * @db_ack_vaddr: cached virtual address for doorbell ack register |
| 73 | * @db_irq: doorbell interrupt |
| 74 | */ |
| 75 | struct pcc_chan_info { |
Sudeep Holla | 0f2591e | 2021-09-17 14:33:49 +0100 | [diff] [blame] | 76 | struct pcc_mbox_chan chan; |
Sudeep Holla | 80b2bdd | 2021-09-17 14:33:46 +0100 | [diff] [blame] | 77 | void __iomem *db_vaddr; |
| 78 | void __iomem *db_ack_vaddr; |
| 79 | int db_irq; |
| 80 | }; |
| 81 | |
Sudeep Holla | 7b6da7f | 2021-09-17 14:33:50 +0100 | [diff] [blame^] | 82 | #define to_pcc_chan_info(c) container_of(c, struct pcc_chan_info, chan) |
Sudeep Holla | 80b2bdd | 2021-09-17 14:33:46 +0100 | [diff] [blame] | 83 | static struct pcc_chan_info *chan_info; |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 84 | static struct mbox_controller pcc_mbox_ctrl = {}; |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 85 | |
Prakash, Prashanth | 8b0f578 | 2016-02-17 13:21:01 -0700 | [diff] [blame] | 86 | /* |
| 87 | * PCC can be used with perf critical drivers such as CPPC |
| 88 | * So it makes sense to locally cache the virtual address and |
| 89 | * use it to read/write to PCC registers such as doorbell register |
| 90 | * |
| 91 | * The below read_register and write_registers are used to read and |
| 92 | * write from perf critical registers such as PCC doorbell register |
| 93 | */ |
| 94 | static int read_register(void __iomem *vaddr, u64 *val, unsigned int bit_width) |
| 95 | { |
| 96 | int ret_val = 0; |
| 97 | |
| 98 | switch (bit_width) { |
| 99 | case 8: |
| 100 | *val = readb(vaddr); |
| 101 | break; |
| 102 | case 16: |
| 103 | *val = readw(vaddr); |
| 104 | break; |
| 105 | case 32: |
| 106 | *val = readl(vaddr); |
| 107 | break; |
| 108 | case 64: |
| 109 | *val = readq(vaddr); |
| 110 | break; |
| 111 | default: |
| 112 | pr_debug("Error: Cannot read register of %u bit width", |
| 113 | bit_width); |
| 114 | ret_val = -EFAULT; |
| 115 | break; |
| 116 | } |
| 117 | return ret_val; |
| 118 | } |
| 119 | |
| 120 | static int write_register(void __iomem *vaddr, u64 val, unsigned int bit_width) |
| 121 | { |
| 122 | int ret_val = 0; |
| 123 | |
| 124 | switch (bit_width) { |
| 125 | case 8: |
| 126 | writeb(val, vaddr); |
| 127 | break; |
| 128 | case 16: |
| 129 | writew(val, vaddr); |
| 130 | break; |
| 131 | case 32: |
| 132 | writel(val, vaddr); |
| 133 | break; |
| 134 | case 64: |
| 135 | writeq(val, vaddr); |
| 136 | break; |
| 137 | default: |
| 138 | pr_debug("Error: Cannot write register of %u bit width", |
| 139 | bit_width); |
| 140 | ret_val = -EFAULT; |
| 141 | break; |
| 142 | } |
| 143 | return ret_val; |
| 144 | } |
| 145 | |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 146 | /** |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 147 | * pcc_map_interrupt - Map a PCC subspace GSI to a linux IRQ number |
| 148 | * @interrupt: GSI number. |
| 149 | * @flags: interrupt flags |
| 150 | * |
| 151 | * Returns: a valid linux IRQ number on success |
| 152 | * 0 or -EINVAL on failure |
| 153 | */ |
| 154 | static int pcc_map_interrupt(u32 interrupt, u32 flags) |
| 155 | { |
| 156 | int trigger, polarity; |
| 157 | |
| 158 | if (!interrupt) |
| 159 | return 0; |
| 160 | |
| 161 | trigger = (flags & ACPI_PCCT_INTERRUPT_MODE) ? ACPI_EDGE_SENSITIVE |
| 162 | : ACPI_LEVEL_SENSITIVE; |
| 163 | |
| 164 | polarity = (flags & ACPI_PCCT_INTERRUPT_POLARITY) ? ACPI_ACTIVE_LOW |
| 165 | : ACPI_ACTIVE_HIGH; |
| 166 | |
| 167 | return acpi_register_gsi(NULL, interrupt, trigger, polarity); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * pcc_mbox_irq - PCC mailbox interrupt handler |
Sudeep Holla | 10dcc2d | 2021-09-17 14:33:44 +0100 | [diff] [blame] | 172 | * @irq: interrupt number |
| 173 | * @p: data/cookie passed from the caller to identify the channel |
| 174 | * |
| 175 | * Returns: IRQ_HANDLED if interrupt is handled or IRQ_NONE if not |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 176 | */ |
| 177 | static irqreturn_t pcc_mbox_irq(int irq, void *p) |
| 178 | { |
| 179 | struct acpi_generic_address *doorbell_ack; |
| 180 | struct acpi_pcct_hw_reduced *pcct_ss; |
Sudeep Holla | 80b2bdd | 2021-09-17 14:33:46 +0100 | [diff] [blame] | 181 | struct pcc_chan_info *pchan; |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 182 | struct mbox_chan *chan = p; |
| 183 | u64 doorbell_ack_preserve; |
| 184 | u64 doorbell_ack_write; |
| 185 | u64 doorbell_ack_val; |
| 186 | int ret; |
| 187 | |
| 188 | pcct_ss = chan->con_priv; |
| 189 | |
| 190 | mbox_chan_received_data(chan, NULL); |
| 191 | |
| 192 | if (pcct_ss->header.type == ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE_TYPE2) { |
| 193 | struct acpi_pcct_hw_reduced_type2 *pcct2_ss = chan->con_priv; |
| 194 | u32 id = chan - pcc_mbox_channels; |
| 195 | |
Sudeep Holla | 80b2bdd | 2021-09-17 14:33:46 +0100 | [diff] [blame] | 196 | pchan = chan_info + id; |
David E. Box | c7a1dfb | 2017-06-05 16:39:08 +0800 | [diff] [blame] | 197 | doorbell_ack = &pcct2_ss->platform_ack_register; |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 198 | doorbell_ack_preserve = pcct2_ss->ack_preserve_mask; |
| 199 | doorbell_ack_write = pcct2_ss->ack_write_mask; |
| 200 | |
Sudeep Holla | 80b2bdd | 2021-09-17 14:33:46 +0100 | [diff] [blame] | 201 | ret = read_register(pchan->db_ack_vaddr, |
| 202 | &doorbell_ack_val, doorbell_ack->bit_width); |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 203 | if (ret) |
| 204 | return IRQ_NONE; |
| 205 | |
Sudeep Holla | 80b2bdd | 2021-09-17 14:33:46 +0100 | [diff] [blame] | 206 | ret = write_register(pchan->db_ack_vaddr, |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 207 | (doorbell_ack_val & doorbell_ack_preserve) |
| 208 | | doorbell_ack_write, |
| 209 | doorbell_ack->bit_width); |
| 210 | if (ret) |
| 211 | return IRQ_NONE; |
| 212 | } |
| 213 | |
| 214 | return IRQ_HANDLED; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * pcc_mbox_request_channel - PCC clients call this function to |
| 219 | * request a pointer to their PCC subspace, from which they |
| 220 | * can get the details of communicating with the remote. |
| 221 | * @cl: Pointer to Mailbox client, so we know where to bind the |
| 222 | * Channel. |
| 223 | * @subspace_id: The PCC Subspace index as parsed in the PCC client |
| 224 | * ACPI package. This is used to lookup the array of PCC |
| 225 | * subspaces as parsed by the PCC Mailbox controller. |
| 226 | * |
Sudeep Holla | 7b6da7f | 2021-09-17 14:33:50 +0100 | [diff] [blame^] | 227 | * Return: Pointer to the PCC Mailbox Channel if successful or ERR_PTR. |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 228 | */ |
Sudeep Holla | 7b6da7f | 2021-09-17 14:33:50 +0100 | [diff] [blame^] | 229 | struct pcc_mbox_chan * |
| 230 | pcc_mbox_request_channel(struct mbox_client *cl, int subspace_id) |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 231 | { |
Sudeep Holla | 80b2bdd | 2021-09-17 14:33:46 +0100 | [diff] [blame] | 232 | struct pcc_chan_info *pchan; |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 233 | struct device *dev = pcc_mbox_ctrl.dev; |
| 234 | struct mbox_chan *chan; |
| 235 | unsigned long flags; |
| 236 | |
Sudeep Holla | 7b6da7f | 2021-09-17 14:33:50 +0100 | [diff] [blame^] | 237 | if (subspace_id < 0 || subspace_id >= pcc_mbox_ctrl.num_chans) |
| 238 | return ERR_PTR(-ENOENT); |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 239 | |
Sudeep Holla | 7b6da7f | 2021-09-17 14:33:50 +0100 | [diff] [blame^] | 240 | pchan = chan_info + subspace_id; |
| 241 | chan = pchan->chan.mchan; |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 242 | if (IS_ERR(chan) || chan->cl) { |
| 243 | dev_err(dev, "Channel not found for idx: %d\n", subspace_id); |
| 244 | return ERR_PTR(-EBUSY); |
| 245 | } |
| 246 | |
| 247 | spin_lock_irqsave(&chan->lock, flags); |
| 248 | chan->msg_free = 0; |
| 249 | chan->msg_count = 0; |
| 250 | chan->active_req = NULL; |
| 251 | chan->cl = cl; |
| 252 | init_completion(&chan->tx_complete); |
| 253 | |
| 254 | if (chan->txdone_method == TXDONE_BY_POLL && cl->knows_txdone) |
Sudeep Holla | 33cd712 | 2017-09-28 11:18:52 +0100 | [diff] [blame] | 255 | chan->txdone_method = TXDONE_BY_ACK; |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 256 | |
Hoan Tran | 6ca595a | 2016-11-14 11:19:02 -0800 | [diff] [blame] | 257 | spin_unlock_irqrestore(&chan->lock, flags); |
| 258 | |
Sudeep Holla | 80b2bdd | 2021-09-17 14:33:46 +0100 | [diff] [blame] | 259 | if (pchan->db_irq > 0) { |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 260 | int rc; |
| 261 | |
Sudeep Holla | 80b2bdd | 2021-09-17 14:33:46 +0100 | [diff] [blame] | 262 | rc = devm_request_irq(dev, pchan->db_irq, pcc_mbox_irq, 0, |
| 263 | MBOX_IRQ_NAME, chan); |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 264 | if (unlikely(rc)) { |
| 265 | dev_err(dev, "failed to register PCC interrupt %d\n", |
Sudeep Holla | 80b2bdd | 2021-09-17 14:33:46 +0100 | [diff] [blame] | 266 | pchan->db_irq); |
Sudeep Holla | 7b6da7f | 2021-09-17 14:33:50 +0100 | [diff] [blame^] | 267 | pcc_mbox_free_channel(&pchan->chan); |
| 268 | return ERR_PTR(rc); |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
Sudeep Holla | 7b6da7f | 2021-09-17 14:33:50 +0100 | [diff] [blame^] | 272 | return &pchan->chan; |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 273 | } |
| 274 | EXPORT_SYMBOL_GPL(pcc_mbox_request_channel); |
| 275 | |
| 276 | /** |
| 277 | * pcc_mbox_free_channel - Clients call this to free their Channel. |
| 278 | * |
Sudeep Holla | 7b6da7f | 2021-09-17 14:33:50 +0100 | [diff] [blame^] | 279 | * @pchan: Pointer to the PCC mailbox channel as returned by |
| 280 | * pcc_mbox_request_channel() |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 281 | */ |
Sudeep Holla | 7b6da7f | 2021-09-17 14:33:50 +0100 | [diff] [blame^] | 282 | void pcc_mbox_free_channel(struct pcc_mbox_chan *pchan) |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 283 | { |
Sudeep Holla | 7b6da7f | 2021-09-17 14:33:50 +0100 | [diff] [blame^] | 284 | struct pcc_chan_info *pchan_info = to_pcc_chan_info(pchan); |
| 285 | struct mbox_chan *chan = pchan->mchan; |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 286 | unsigned long flags; |
| 287 | |
| 288 | if (!chan || !chan->cl) |
| 289 | return; |
| 290 | |
Sudeep Holla | 7b6da7f | 2021-09-17 14:33:50 +0100 | [diff] [blame^] | 291 | if (pchan_info->db_irq > 0) |
| 292 | devm_free_irq(chan->mbox->dev, pchan_info->db_irq, chan); |
Hoan Tran | 6ca595a | 2016-11-14 11:19:02 -0800 | [diff] [blame] | 293 | |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 294 | spin_lock_irqsave(&chan->lock, flags); |
| 295 | chan->cl = NULL; |
| 296 | chan->active_req = NULL; |
Sudeep Holla | 33cd712 | 2017-09-28 11:18:52 +0100 | [diff] [blame] | 297 | if (chan->txdone_method == TXDONE_BY_ACK) |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 298 | chan->txdone_method = TXDONE_BY_POLL; |
| 299 | |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 300 | spin_unlock_irqrestore(&chan->lock, flags); |
| 301 | } |
| 302 | EXPORT_SYMBOL_GPL(pcc_mbox_free_channel); |
| 303 | |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 304 | /** |
Ashwin Chaugule | 33350e6 | 2015-01-27 16:03:57 -0500 | [diff] [blame] | 305 | * pcc_send_data - Called from Mailbox Controller code. Used |
| 306 | * here only to ring the channel doorbell. The PCC client |
| 307 | * specific read/write is done in the client driver in |
| 308 | * order to maintain atomicity over PCC channel once |
| 309 | * OS has control over it. See above for flow of operations. |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 310 | * @chan: Pointer to Mailbox channel over which to send data. |
Ashwin Chaugule | 33350e6 | 2015-01-27 16:03:57 -0500 | [diff] [blame] | 311 | * @data: Client specific data written over channel. Used here |
| 312 | * only for debug after PCC transaction completes. |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 313 | * |
| 314 | * Return: Err if something failed else 0 for success. |
| 315 | */ |
| 316 | static int pcc_send_data(struct mbox_chan *chan, void *data) |
| 317 | { |
| 318 | struct acpi_pcct_hw_reduced *pcct_ss = chan->con_priv; |
Prakash, Prashanth | 8b0f578 | 2016-02-17 13:21:01 -0700 | [diff] [blame] | 319 | struct acpi_generic_address *doorbell; |
Sudeep Holla | 80b2bdd | 2021-09-17 14:33:46 +0100 | [diff] [blame] | 320 | struct pcc_chan_info *pchan; |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 321 | u64 doorbell_preserve; |
| 322 | u64 doorbell_val; |
| 323 | u64 doorbell_write; |
Prakash, Prashanth | 8b0f578 | 2016-02-17 13:21:01 -0700 | [diff] [blame] | 324 | u32 id = chan - pcc_mbox_channels; |
| 325 | int ret = 0; |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 326 | |
Prakash, Prashanth | 8b0f578 | 2016-02-17 13:21:01 -0700 | [diff] [blame] | 327 | if (id >= pcc_mbox_ctrl.num_chans) { |
| 328 | pr_debug("pcc_send_data: Invalid mbox_chan passed\n"); |
| 329 | return -ENOENT; |
| 330 | } |
| 331 | |
Sudeep Holla | 80b2bdd | 2021-09-17 14:33:46 +0100 | [diff] [blame] | 332 | pchan = chan_info + id; |
Prakash, Prashanth | 8b0f578 | 2016-02-17 13:21:01 -0700 | [diff] [blame] | 333 | doorbell = &pcct_ss->doorbell_register; |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 334 | doorbell_preserve = pcct_ss->preserve_mask; |
| 335 | doorbell_write = pcct_ss->write_mask; |
| 336 | |
Ashwin Chaugule | 33350e6 | 2015-01-27 16:03:57 -0500 | [diff] [blame] | 337 | /* Sync notification from OS to Platform. */ |
Sudeep Holla | 80b2bdd | 2021-09-17 14:33:46 +0100 | [diff] [blame] | 338 | if (pchan->db_vaddr) { |
| 339 | ret = read_register(pchan->db_vaddr, &doorbell_val, |
| 340 | doorbell->bit_width); |
Prakash, Prashanth | 8b0f578 | 2016-02-17 13:21:01 -0700 | [diff] [blame] | 341 | if (ret) |
| 342 | return ret; |
Sudeep Holla | 80b2bdd | 2021-09-17 14:33:46 +0100 | [diff] [blame] | 343 | ret = write_register(pchan->db_vaddr, |
| 344 | (doorbell_val & doorbell_preserve) |
| 345 | | doorbell_write, doorbell->bit_width); |
Prakash, Prashanth | 8b0f578 | 2016-02-17 13:21:01 -0700 | [diff] [blame] | 346 | } else { |
| 347 | ret = acpi_read(&doorbell_val, doorbell); |
| 348 | if (ret) |
| 349 | return ret; |
| 350 | ret = acpi_write((doorbell_val & doorbell_preserve) | doorbell_write, |
| 351 | doorbell); |
| 352 | } |
| 353 | return ret; |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 354 | } |
| 355 | |
Andrew Bresticker | 05ae797 | 2015-05-04 10:36:35 -0700 | [diff] [blame] | 356 | static const struct mbox_chan_ops pcc_chan_ops = { |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 357 | .send_data = pcc_send_data, |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 358 | }; |
| 359 | |
| 360 | /** |
Sudeep Holla | 10dcc2d | 2021-09-17 14:33:44 +0100 | [diff] [blame] | 361 | * parse_pcc_subspace - Count PCC subspaces defined |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 362 | * @header: Pointer to the ACPI subtable header under the PCCT. |
| 363 | * @end: End of subtable entry. |
| 364 | * |
Al Stone | 8f8027c | 2018-05-16 16:01:41 -0600 | [diff] [blame] | 365 | * Return: If we find a PCC subspace entry of a valid type, return 0. |
| 366 | * Otherwise, return -EINVAL. |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 367 | * |
| 368 | * This gets called for each entry in the PCC table. |
| 369 | */ |
Keith Busch | 60574d1 | 2019-03-11 14:55:57 -0600 | [diff] [blame] | 370 | static int parse_pcc_subspace(union acpi_subtable_headers *header, |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 371 | const unsigned long end) |
| 372 | { |
Al Stone | 8f8027c | 2018-05-16 16:01:41 -0600 | [diff] [blame] | 373 | struct acpi_pcct_subspace *ss = (struct acpi_pcct_subspace *) header; |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 374 | |
Al Stone | 8f8027c | 2018-05-16 16:01:41 -0600 | [diff] [blame] | 375 | if (ss->header.type < ACPI_PCCT_TYPE_RESERVED) |
| 376 | return 0; |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 377 | |
Al Stone | 8f8027c | 2018-05-16 16:01:41 -0600 | [diff] [blame] | 378 | return -EINVAL; |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /** |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 382 | * pcc_parse_subspace_irq - Parse the PCC IRQ and PCC ACK register |
Sudeep Holla | 319bfb3 | 2021-09-17 14:33:47 +0100 | [diff] [blame] | 383 | * |
| 384 | * @pchan: Pointer to the PCC channel info structure. |
| 385 | * @pcct_entry: Pointer to the ACPI subtable header. |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 386 | * |
| 387 | * Return: 0 for Success, else errno. |
| 388 | * |
Sudeep Holla | 319bfb3 | 2021-09-17 14:33:47 +0100 | [diff] [blame] | 389 | * There should be one entry per PCC channel. This gets called for each |
| 390 | * entry in the PCC table. This uses PCCY Type1 structure for all applicable |
| 391 | * types(Type 1-4) to fetch irq |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 392 | */ |
Sudeep Holla | 319bfb3 | 2021-09-17 14:33:47 +0100 | [diff] [blame] | 393 | static int pcc_parse_subspace_irq(struct pcc_chan_info *pchan, |
| 394 | struct acpi_subtable_header *pcct_entry) |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 395 | { |
Sudeep Holla | 319bfb3 | 2021-09-17 14:33:47 +0100 | [diff] [blame] | 396 | struct acpi_pcct_hw_reduced *pcct_ss; |
Sudeep Holla | 80b2bdd | 2021-09-17 14:33:46 +0100 | [diff] [blame] | 397 | |
Sudeep Holla | 319bfb3 | 2021-09-17 14:33:47 +0100 | [diff] [blame] | 398 | if (pcct_entry->type < ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE || |
| 399 | pcct_entry->type > ACPI_PCCT_TYPE_EXT_PCC_SLAVE_SUBSPACE) |
| 400 | return 0; |
| 401 | |
| 402 | pcct_ss = (struct acpi_pcct_hw_reduced *)pcct_entry; |
Sudeep Holla | 80b2bdd | 2021-09-17 14:33:46 +0100 | [diff] [blame] | 403 | pchan->db_irq = pcc_map_interrupt(pcct_ss->platform_interrupt, |
| 404 | (u32)pcct_ss->flags); |
| 405 | if (pchan->db_irq <= 0) { |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 406 | pr_err("PCC GSI %d not registered\n", |
David E. Box | c7a1dfb | 2017-06-05 16:39:08 +0800 | [diff] [blame] | 407 | pcct_ss->platform_interrupt); |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 408 | return -EINVAL; |
| 409 | } |
| 410 | |
Sudeep Holla | 319bfb3 | 2021-09-17 14:33:47 +0100 | [diff] [blame] | 411 | if (pcct_ss->header.type == ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE_TYPE2) { |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 412 | struct acpi_pcct_hw_reduced_type2 *pcct2_ss = (void *)pcct_ss; |
| 413 | |
Sudeep Holla | 80b2bdd | 2021-09-17 14:33:46 +0100 | [diff] [blame] | 414 | pchan->db_ack_vaddr = |
| 415 | acpi_os_ioremap(pcct2_ss->platform_ack_register.address, |
| 416 | pcct2_ss->platform_ack_register.bit_width / 8); |
| 417 | if (!pchan->db_ack_vaddr) { |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 418 | pr_err("Failed to ioremap PCC ACK register\n"); |
| 419 | return -ENOMEM; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | /** |
Sudeep Holla | 4e3c96f | 2021-09-17 14:33:48 +0100 | [diff] [blame] | 427 | * pcc_parse_subspace_db_reg - Parse the PCC doorbell register |
| 428 | * |
| 429 | * @pchan: Pointer to the PCC channel info structure. |
| 430 | * @pcct_entry: Pointer to the ACPI subtable header. |
| 431 | * |
| 432 | */ |
| 433 | static void pcc_parse_subspace_db_reg(struct pcc_chan_info *pchan, |
| 434 | struct acpi_subtable_header *pcct_entry) |
| 435 | { |
| 436 | struct acpi_pcct_subspace *pcct_ss; |
| 437 | struct acpi_generic_address *db_reg; |
| 438 | |
| 439 | pcct_ss = (struct acpi_pcct_subspace *)pcct_entry; |
| 440 | |
| 441 | /* If doorbell is in system memory cache the virt address */ |
| 442 | db_reg = &pcct_ss->doorbell_register; |
| 443 | if (db_reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) |
| 444 | pchan->db_vaddr = acpi_os_ioremap(db_reg->address, |
| 445 | db_reg->bit_width / 8); |
| 446 | } |
| 447 | |
| 448 | /** |
Sudeep Holla | 0f2591e | 2021-09-17 14:33:49 +0100 | [diff] [blame] | 449 | * pcc_parse_subspace_shmem - Parse the PCC Shared Memory Region information |
| 450 | * |
| 451 | * @pchan: Pointer to the PCC channel info structure. |
| 452 | * @pcct_entry: Pointer to the ACPI subtable header. |
| 453 | * |
| 454 | */ |
| 455 | static void pcc_parse_subspace_shmem(struct pcc_chan_info *pchan, |
| 456 | struct acpi_subtable_header *pcct_entry) |
| 457 | { |
| 458 | struct acpi_pcct_subspace *pcct_ss; |
| 459 | |
| 460 | pcct_ss = (struct acpi_pcct_subspace *)pcct_entry; |
| 461 | |
| 462 | pchan->chan.shmem_base_addr = pcct_ss->base_address; |
| 463 | pchan->chan.shmem_size = pcct_ss->length; |
| 464 | pchan->chan.latency = pcct_ss->latency; |
| 465 | pchan->chan.max_access_rate = pcct_ss->max_access_rate; |
| 466 | pchan->chan.min_turnaround_time = pcct_ss->min_turnaround_time; |
| 467 | } |
| 468 | |
| 469 | /** |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 470 | * acpi_pcc_probe - Parse the ACPI tree for the PCCT. |
| 471 | * |
| 472 | * Return: 0 for Success, else errno. |
| 473 | */ |
| 474 | static int __init acpi_pcc_probe(void) |
| 475 | { |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 476 | struct acpi_table_header *pcct_tbl; |
| 477 | struct acpi_subtable_header *pcct_entry; |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 478 | struct acpi_table_pcct *acpi_pcct_tbl; |
Al Stone | 8f8027c | 2018-05-16 16:01:41 -0600 | [diff] [blame] | 479 | struct acpi_subtable_proc proc[ACPI_PCCT_TYPE_RESERVED]; |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 480 | int count, i, rc; |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 481 | acpi_status status = AE_OK; |
| 482 | |
| 483 | /* Search for PCCT */ |
Lv Zheng | 6b11d1d | 2016-12-14 15:04:39 +0800 | [diff] [blame] | 484 | status = acpi_get_table(ACPI_SIG_PCCT, 0, &pcct_tbl); |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 485 | |
Punit Agrawal | 66ed4ca | 2017-08-01 13:43:57 +0100 | [diff] [blame] | 486 | if (ACPI_FAILURE(status) || !pcct_tbl) |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 487 | return -ENODEV; |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 488 | |
Al Stone | 8f8027c | 2018-05-16 16:01:41 -0600 | [diff] [blame] | 489 | /* Set up the subtable handlers */ |
| 490 | for (i = ACPI_PCCT_TYPE_GENERIC_SUBSPACE; |
| 491 | i < ACPI_PCCT_TYPE_RESERVED; i++) { |
| 492 | proc[i].id = i; |
| 493 | proc[i].count = 0; |
| 494 | proc[i].handler = parse_pcc_subspace; |
| 495 | } |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 496 | |
Al Stone | 8f8027c | 2018-05-16 16:01:41 -0600 | [diff] [blame] | 497 | count = acpi_table_parse_entries_array(ACPI_SIG_PCCT, |
| 498 | sizeof(struct acpi_table_pcct), proc, |
| 499 | ACPI_PCCT_TYPE_RESERVED, MAX_PCC_SUBSPACES); |
David Arcari | afd0b1f | 2018-08-27 15:19:08 -0400 | [diff] [blame] | 500 | if (count <= 0 || count > MAX_PCC_SUBSPACES) { |
| 501 | if (count < 0) |
| 502 | pr_warn("Error parsing PCC subspaces from PCCT\n"); |
| 503 | else |
| 504 | pr_warn("Invalid PCCT: %d PCC subspaces\n", count); |
Hanjun Guo | 425ab03 | 2020-07-22 17:40:40 +0800 | [diff] [blame] | 505 | |
| 506 | rc = -EINVAL; |
| 507 | goto err_put_pcct; |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 508 | } |
| 509 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 510 | pcc_mbox_channels = kcalloc(count, sizeof(struct mbox_chan), |
| 511 | GFP_KERNEL); |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 512 | if (!pcc_mbox_channels) { |
| 513 | pr_err("Could not allocate space for PCC mbox channels\n"); |
Hanjun Guo | 425ab03 | 2020-07-22 17:40:40 +0800 | [diff] [blame] | 514 | rc = -ENOMEM; |
| 515 | goto err_put_pcct; |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 516 | } |
| 517 | |
Sudeep Holla | 80b2bdd | 2021-09-17 14:33:46 +0100 | [diff] [blame] | 518 | chan_info = kcalloc(count, sizeof(*chan_info), GFP_KERNEL); |
| 519 | if (!chan_info) { |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 520 | rc = -ENOMEM; |
| 521 | goto err_free_mbox; |
| 522 | } |
| 523 | |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 524 | /* Point to the first PCC subspace entry */ |
| 525 | pcct_entry = (struct acpi_subtable_header *) ( |
| 526 | (unsigned long) pcct_tbl + sizeof(struct acpi_table_pcct)); |
| 527 | |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 528 | acpi_pcct_tbl = (struct acpi_table_pcct *) pcct_tbl; |
| 529 | if (acpi_pcct_tbl->flags & ACPI_PCCT_DOORBELL) |
| 530 | pcc_mbox_ctrl.txdone_irq = true; |
| 531 | |
Al Stone | 8f8027c | 2018-05-16 16:01:41 -0600 | [diff] [blame] | 532 | for (i = 0; i < count; i++) { |
Sudeep Holla | 80b2bdd | 2021-09-17 14:33:46 +0100 | [diff] [blame] | 533 | struct pcc_chan_info *pchan = chan_info + i; |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 534 | pcc_mbox_channels[i].con_priv = pcct_entry; |
Prakash, Prashanth | 8b0f578 | 2016-02-17 13:21:01 -0700 | [diff] [blame] | 535 | |
Sudeep Holla | 0f2591e | 2021-09-17 14:33:49 +0100 | [diff] [blame] | 536 | pchan->chan.mchan = &pcc_mbox_channels[i]; |
| 537 | |
Sudeep Holla | 319bfb3 | 2021-09-17 14:33:47 +0100 | [diff] [blame] | 538 | if (pcc_mbox_ctrl.txdone_irq) { |
| 539 | rc = pcc_parse_subspace_irq(pchan, pcct_entry); |
| 540 | if (rc < 0) |
| 541 | goto err; |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 542 | } |
Sudeep Holla | 4e3c96f | 2021-09-17 14:33:48 +0100 | [diff] [blame] | 543 | pcc_parse_subspace_db_reg(pchan, pcct_entry); |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 544 | |
Sudeep Holla | 0f2591e | 2021-09-17 14:33:49 +0100 | [diff] [blame] | 545 | pcc_parse_subspace_shmem(pchan, pcct_entry); |
| 546 | |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 547 | pcct_entry = (struct acpi_subtable_header *) |
| 548 | ((unsigned long) pcct_entry + pcct_entry->length); |
| 549 | } |
| 550 | |
Al Stone | 8f8027c | 2018-05-16 16:01:41 -0600 | [diff] [blame] | 551 | pcc_mbox_ctrl.num_chans = count; |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 552 | |
| 553 | pr_info("Detected %d PCC Subspaces\n", pcc_mbox_ctrl.num_chans); |
| 554 | |
| 555 | return 0; |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 556 | |
| 557 | err: |
Sudeep Holla | 80b2bdd | 2021-09-17 14:33:46 +0100 | [diff] [blame] | 558 | kfree(chan_info); |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 559 | err_free_mbox: |
| 560 | kfree(pcc_mbox_channels); |
Hanjun Guo | 425ab03 | 2020-07-22 17:40:40 +0800 | [diff] [blame] | 561 | err_put_pcct: |
| 562 | acpi_put_table(pcct_tbl); |
hotran | aca314e | 2016-08-15 17:14:05 -0700 | [diff] [blame] | 563 | return rc; |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | /** |
| 567 | * pcc_mbox_probe - Called when we find a match for the |
| 568 | * PCCT platform device. This is purely used to represent |
| 569 | * the PCCT as a virtual device for registering with the |
| 570 | * generic Mailbox framework. |
| 571 | * |
| 572 | * @pdev: Pointer to platform device returned when a match |
| 573 | * is found. |
| 574 | * |
| 575 | * Return: 0 for Success, else errno. |
| 576 | */ |
| 577 | static int pcc_mbox_probe(struct platform_device *pdev) |
| 578 | { |
| 579 | int ret = 0; |
| 580 | |
| 581 | pcc_mbox_ctrl.chans = pcc_mbox_channels; |
| 582 | pcc_mbox_ctrl.ops = &pcc_chan_ops; |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 583 | pcc_mbox_ctrl.dev = &pdev->dev; |
| 584 | |
| 585 | pr_info("Registering PCC driver as Mailbox controller\n"); |
| 586 | ret = mbox_controller_register(&pcc_mbox_ctrl); |
| 587 | |
| 588 | if (ret) { |
| 589 | pr_err("Err registering PCC as Mailbox controller: %d\n", ret); |
| 590 | ret = -ENODEV; |
| 591 | } |
| 592 | |
| 593 | return ret; |
| 594 | } |
| 595 | |
Jason Yan | 00d9990 | 2020-04-03 11:52:08 +0800 | [diff] [blame] | 596 | static struct platform_driver pcc_mbox_driver = { |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 597 | .probe = pcc_mbox_probe, |
| 598 | .driver = { |
| 599 | .name = "PCCT", |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 600 | }, |
| 601 | }; |
| 602 | |
| 603 | static int __init pcc_init(void) |
| 604 | { |
| 605 | int ret; |
| 606 | struct platform_device *pcc_pdev; |
| 607 | |
| 608 | if (acpi_disabled) |
| 609 | return -ENODEV; |
| 610 | |
| 611 | /* Check if PCC support is available. */ |
| 612 | ret = acpi_pcc_probe(); |
| 613 | |
| 614 | if (ret) { |
Rafael J. Wysocki | efd756d | 2015-02-05 00:40:08 +0100 | [diff] [blame] | 615 | pr_debug("ACPI PCC probe failed.\n"); |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 616 | return -ENODEV; |
| 617 | } |
| 618 | |
| 619 | pcc_pdev = platform_create_bundle(&pcc_mbox_driver, |
| 620 | pcc_mbox_probe, NULL, 0, NULL, 0); |
| 621 | |
Wei Yongjun | 356d5d2 | 2015-01-14 09:10:56 +0800 | [diff] [blame] | 622 | if (IS_ERR(pcc_pdev)) { |
Rafael J. Wysocki | efd756d | 2015-02-05 00:40:08 +0100 | [diff] [blame] | 623 | pr_debug("Err creating PCC platform bundle\n"); |
Wei Yongjun | 356d5d2 | 2015-01-14 09:10:56 +0800 | [diff] [blame] | 624 | return PTR_ERR(pcc_pdev); |
Ashwin Chaugule | 86c22f8 | 2014-11-12 19:59:38 -0500 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | return 0; |
| 628 | } |
Ashwin Chaugule | d3c68f2 | 2015-08-05 09:40:24 -0400 | [diff] [blame] | 629 | |
| 630 | /* |
| 631 | * Make PCC init postcore so that users of this mailbox |
| 632 | * such as the ACPI Processor driver have it available |
| 633 | * at their init. |
| 634 | */ |
| 635 | postcore_initcall(pcc_init); |