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