Merge remote-tracking branch 'stable/linux-5.15.y' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / drivers / mailbox / pcc.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *      Copyright (C) 2014 Linaro Ltd.
4  *      Author: Ashwin Chaugule <ashwin.chaugule@linaro.org>
5  *
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
14  *  is also specified in each PCC table entry.
15  *
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.
35  *  * If command completes, then writes have succeeded and it can release
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
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>
53 #include <linux/interrupt.h>
54 #include <linux/list.h>
55 #include <linux/platform_device.h>
56 #include <linux/mailbox_controller.h>
57 #include <linux/mailbox_client.h>
58 #include <linux/io-64-nonatomic-lo-hi.h>
59 #include <acpi/pcc.h>
60
61 #include "mailbox.h"
62
63 #define MBOX_IRQ_NAME           "pcc-mbox"
64
65 static struct mbox_chan *pcc_mbox_channels;
66
67 /* Array of cached virtual address for doorbell registers */
68 static void __iomem **pcc_doorbell_vaddr;
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;
73
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 {
85         if (id < 0 || id >= pcc_mbox_ctrl.num_chans)
86                 return ERR_PTR(-ENOENT);
87
88         return &pcc_mbox_channels[id];
89 }
90
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
151 /**
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
177  */
178 static irqreturn_t pcc_mbox_irq(int irq, void *p)
179 {
180         struct acpi_generic_address *doorbell_ack;
181         struct acpi_pcct_hw_reduced *pcct_ss;
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
196                 doorbell_ack = &pcct2_ss->platform_ack_register;
197                 doorbell_ack_preserve = pcct2_ss->ack_preserve_mask;
198                 doorbell_ack_write = pcct2_ss->ack_write_mask;
199
200                 ret = read_register(pcc_doorbell_ack_vaddr[id],
201                                     &doorbell_ack_val,
202                                     doorbell_ack->bit_width);
203                 if (ret)
204                         return IRQ_NONE;
205
206                 ret = write_register(pcc_doorbell_ack_vaddr[id],
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  *
227  * Return: Pointer to the Mailbox Channel if successful or
228  *              ERR_PTR.
229  */
230 struct mbox_chan *pcc_mbox_request_channel(struct mbox_client *cl,
231                 int subspace_id)
232 {
233         struct device *dev = pcc_mbox_ctrl.dev;
234         struct mbox_chan *chan;
235         unsigned long flags;
236
237         /*
238          * Each PCC Subspace is a Mailbox Channel.
239          * The PCC Clients get their PCC Subspace ID
240          * from their own tables and pass it here.
241          * This returns a pointer to the PCC subspace
242          * for the Client to operate on.
243          */
244         chan = get_pcc_channel(subspace_id);
245
246         if (IS_ERR(chan) || chan->cl) {
247                 dev_err(dev, "Channel not found for idx: %d\n", subspace_id);
248                 return ERR_PTR(-EBUSY);
249         }
250
251         spin_lock_irqsave(&chan->lock, flags);
252         chan->msg_free = 0;
253         chan->msg_count = 0;
254         chan->active_req = NULL;
255         chan->cl = cl;
256         init_completion(&chan->tx_complete);
257
258         if (chan->txdone_method == TXDONE_BY_POLL && cl->knows_txdone)
259                 chan->txdone_method = TXDONE_BY_ACK;
260
261         spin_unlock_irqrestore(&chan->lock, flags);
262
263         if (pcc_doorbell_irq[subspace_id] > 0) {
264                 int rc;
265
266                 rc = devm_request_irq(dev, pcc_doorbell_irq[subspace_id],
267                                       pcc_mbox_irq, 0, MBOX_IRQ_NAME, chan);
268                 if (unlikely(rc)) {
269                         dev_err(dev, "failed to register PCC interrupt %d\n",
270                                 pcc_doorbell_irq[subspace_id]);
271                         pcc_mbox_free_channel(chan);
272                         chan = ERR_PTR(rc);
273                 }
274         }
275
276         return chan;
277 }
278 EXPORT_SYMBOL_GPL(pcc_mbox_request_channel);
279
280 /**
281  * pcc_mbox_free_channel - Clients call this to free their Channel.
282  *
283  * @chan: Pointer to the mailbox channel as returned by
284  *              pcc_mbox_request_channel()
285  */
286 void pcc_mbox_free_channel(struct mbox_chan *chan)
287 {
288         u32 id = chan - pcc_mbox_channels;
289         unsigned long flags;
290
291         if (!chan || !chan->cl)
292                 return;
293
294         if (id >= pcc_mbox_ctrl.num_chans) {
295                 pr_debug("pcc_mbox_free_channel: Invalid mbox_chan passed\n");
296                 return;
297         }
298
299         if (pcc_doorbell_irq[id] > 0)
300                 devm_free_irq(chan->mbox->dev, pcc_doorbell_irq[id], chan);
301
302         spin_lock_irqsave(&chan->lock, flags);
303         chan->cl = NULL;
304         chan->active_req = NULL;
305         if (chan->txdone_method == TXDONE_BY_ACK)
306                 chan->txdone_method = TXDONE_BY_POLL;
307
308         spin_unlock_irqrestore(&chan->lock, flags);
309 }
310 EXPORT_SYMBOL_GPL(pcc_mbox_free_channel);
311
312 /**
313  * pcc_send_data - Called from Mailbox Controller code. Used
314  *              here only to ring the channel doorbell. The PCC client
315  *              specific read/write is done in the client driver in
316  *              order to maintain atomicity over PCC channel once
317  *              OS has control over it. See above for flow of operations.
318  * @chan: Pointer to Mailbox channel over which to send data.
319  * @data: Client specific data written over channel. Used here
320  *              only for debug after PCC transaction completes.
321  *
322  * Return: Err if something failed else 0 for success.
323  */
324 static int pcc_send_data(struct mbox_chan *chan, void *data)
325 {
326         struct acpi_pcct_hw_reduced *pcct_ss = chan->con_priv;
327         struct acpi_generic_address *doorbell;
328         u64 doorbell_preserve;
329         u64 doorbell_val;
330         u64 doorbell_write;
331         u32 id = chan - pcc_mbox_channels;
332         int ret = 0;
333
334         if (id >= pcc_mbox_ctrl.num_chans) {
335                 pr_debug("pcc_send_data: Invalid mbox_chan passed\n");
336                 return -ENOENT;
337         }
338
339         doorbell = &pcct_ss->doorbell_register;
340         doorbell_preserve = pcct_ss->preserve_mask;
341         doorbell_write = pcct_ss->write_mask;
342
343         /* Sync notification from OS to Platform. */
344         if (pcc_doorbell_vaddr[id]) {
345                 ret = read_register(pcc_doorbell_vaddr[id], &doorbell_val,
346                         doorbell->bit_width);
347                 if (ret)
348                         return ret;
349                 ret = write_register(pcc_doorbell_vaddr[id],
350                         (doorbell_val & doorbell_preserve) | doorbell_write,
351                         doorbell->bit_width);
352         } else {
353                 ret = acpi_read(&doorbell_val, doorbell);
354                 if (ret)
355                         return ret;
356                 ret = acpi_write((doorbell_val & doorbell_preserve) | doorbell_write,
357                         doorbell);
358         }
359         return ret;
360 }
361
362 static const struct mbox_chan_ops pcc_chan_ops = {
363         .send_data = pcc_send_data,
364 };
365
366 /**
367  * parse_pcc_subspaces -- Count PCC subspaces defined
368  * @header: Pointer to the ACPI subtable header under the PCCT.
369  * @end: End of subtable entry.
370  *
371  * Return: If we find a PCC subspace entry of a valid type, return 0.
372  *      Otherwise, return -EINVAL.
373  *
374  * This gets called for each entry in the PCC table.
375  */
376 static int parse_pcc_subspace(union acpi_subtable_headers *header,
377                 const unsigned long end)
378 {
379         struct acpi_pcct_subspace *ss = (struct acpi_pcct_subspace *) header;
380
381         if (ss->header.type < ACPI_PCCT_TYPE_RESERVED)
382                 return 0;
383
384         return -EINVAL;
385 }
386
387 /**
388  * pcc_parse_subspace_irq - Parse the PCC IRQ and PCC ACK register
389  *              There should be one entry per PCC client.
390  * @id: PCC subspace index.
391  * @pcct_ss: Pointer to the ACPI subtable header under the PCCT.
392  *
393  * Return: 0 for Success, else errno.
394  *
395  * This gets called for each entry in the PCC table.
396  */
397 static int pcc_parse_subspace_irq(int id,
398                                   struct acpi_pcct_hw_reduced *pcct_ss)
399 {
400         pcc_doorbell_irq[id] = pcc_map_interrupt(pcct_ss->platform_interrupt,
401                                                  (u32)pcct_ss->flags);
402         if (pcc_doorbell_irq[id] <= 0) {
403                 pr_err("PCC GSI %d not registered\n",
404                        pcct_ss->platform_interrupt);
405                 return -EINVAL;
406         }
407
408         if (pcct_ss->header.type
409                 == ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE_TYPE2) {
410                 struct acpi_pcct_hw_reduced_type2 *pcct2_ss = (void *)pcct_ss;
411
412                 pcc_doorbell_ack_vaddr[id] = acpi_os_ioremap(
413                                 pcct2_ss->platform_ack_register.address,
414                                 pcct2_ss->platform_ack_register.bit_width / 8);
415                 if (!pcc_doorbell_ack_vaddr[id]) {
416                         pr_err("Failed to ioremap PCC ACK register\n");
417                         return -ENOMEM;
418                 }
419         }
420
421         return 0;
422 }
423
424 /**
425  * acpi_pcc_probe - Parse the ACPI tree for the PCCT.
426  *
427  * Return: 0 for Success, else errno.
428  */
429 static int __init acpi_pcc_probe(void)
430 {
431         struct acpi_table_header *pcct_tbl;
432         struct acpi_subtable_header *pcct_entry;
433         struct acpi_table_pcct *acpi_pcct_tbl;
434         struct acpi_subtable_proc proc[ACPI_PCCT_TYPE_RESERVED];
435         int count, i, rc;
436         acpi_status status = AE_OK;
437
438         /* Search for PCCT */
439         status = acpi_get_table(ACPI_SIG_PCCT, 0, &pcct_tbl);
440
441         if (ACPI_FAILURE(status) || !pcct_tbl)
442                 return -ENODEV;
443
444         /* Set up the subtable handlers */
445         for (i = ACPI_PCCT_TYPE_GENERIC_SUBSPACE;
446              i < ACPI_PCCT_TYPE_RESERVED; i++) {
447                 proc[i].id = i;
448                 proc[i].count = 0;
449                 proc[i].handler = parse_pcc_subspace;
450         }
451
452         count = acpi_table_parse_entries_array(ACPI_SIG_PCCT,
453                         sizeof(struct acpi_table_pcct), proc,
454                         ACPI_PCCT_TYPE_RESERVED, MAX_PCC_SUBSPACES);
455         if (count <= 0 || count > MAX_PCC_SUBSPACES) {
456                 if (count < 0)
457                         pr_warn("Error parsing PCC subspaces from PCCT\n");
458                 else
459                         pr_warn("Invalid PCCT: %d PCC subspaces\n", count);
460
461                 rc = -EINVAL;
462                 goto err_put_pcct;
463         }
464
465         pcc_mbox_channels = kcalloc(count, sizeof(struct mbox_chan),
466                                     GFP_KERNEL);
467         if (!pcc_mbox_channels) {
468                 pr_err("Could not allocate space for PCC mbox channels\n");
469                 rc = -ENOMEM;
470                 goto err_put_pcct;
471         }
472
473         pcc_doorbell_vaddr = kcalloc(count, sizeof(void *), GFP_KERNEL);
474         if (!pcc_doorbell_vaddr) {
475                 rc = -ENOMEM;
476                 goto err_free_mbox;
477         }
478
479         pcc_doorbell_ack_vaddr = kcalloc(count, sizeof(void *), GFP_KERNEL);
480         if (!pcc_doorbell_ack_vaddr) {
481                 rc = -ENOMEM;
482                 goto err_free_db_vaddr;
483         }
484
485         pcc_doorbell_irq = kcalloc(count, sizeof(int), GFP_KERNEL);
486         if (!pcc_doorbell_irq) {
487                 rc = -ENOMEM;
488                 goto err_free_db_ack_vaddr;
489         }
490
491         /* Point to the first PCC subspace entry */
492         pcct_entry = (struct acpi_subtable_header *) (
493                 (unsigned long) pcct_tbl + sizeof(struct acpi_table_pcct));
494
495         acpi_pcct_tbl = (struct acpi_table_pcct *) pcct_tbl;
496         if (acpi_pcct_tbl->flags & ACPI_PCCT_DOORBELL)
497                 pcc_mbox_ctrl.txdone_irq = true;
498
499         for (i = 0; i < count; i++) {
500                 struct acpi_generic_address *db_reg;
501                 struct acpi_pcct_subspace *pcct_ss;
502                 pcc_mbox_channels[i].con_priv = pcct_entry;
503
504                 if (pcct_entry->type == ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE ||
505                     pcct_entry->type == ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE_TYPE2) {
506                         struct acpi_pcct_hw_reduced *pcct_hrss;
507
508                         pcct_hrss = (struct acpi_pcct_hw_reduced *) pcct_entry;
509
510                         if (pcc_mbox_ctrl.txdone_irq) {
511                                 rc = pcc_parse_subspace_irq(i, pcct_hrss);
512                                 if (rc < 0)
513                                         goto err;
514                         }
515                 }
516                 pcct_ss = (struct acpi_pcct_subspace *) pcct_entry;
517
518                 /* If doorbell is in system memory cache the virt address */
519                 db_reg = &pcct_ss->doorbell_register;
520                 if (db_reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
521                         pcc_doorbell_vaddr[i] = acpi_os_ioremap(db_reg->address,
522                                                         db_reg->bit_width/8);
523                 pcct_entry = (struct acpi_subtable_header *)
524                         ((unsigned long) pcct_entry + pcct_entry->length);
525         }
526
527         pcc_mbox_ctrl.num_chans = count;
528
529         pr_info("Detected %d PCC Subspaces\n", pcc_mbox_ctrl.num_chans);
530
531         return 0;
532
533 err:
534         kfree(pcc_doorbell_irq);
535 err_free_db_ack_vaddr:
536         kfree(pcc_doorbell_ack_vaddr);
537 err_free_db_vaddr:
538         kfree(pcc_doorbell_vaddr);
539 err_free_mbox:
540         kfree(pcc_mbox_channels);
541 err_put_pcct:
542         acpi_put_table(pcct_tbl);
543         return rc;
544 }
545
546 /**
547  * pcc_mbox_probe - Called when we find a match for the
548  *      PCCT platform device. This is purely used to represent
549  *      the PCCT as a virtual device for registering with the
550  *      generic Mailbox framework.
551  *
552  * @pdev: Pointer to platform device returned when a match
553  *      is found.
554  *
555  *      Return: 0 for Success, else errno.
556  */
557 static int pcc_mbox_probe(struct platform_device *pdev)
558 {
559         int ret = 0;
560
561         pcc_mbox_ctrl.chans = pcc_mbox_channels;
562         pcc_mbox_ctrl.ops = &pcc_chan_ops;
563         pcc_mbox_ctrl.dev = &pdev->dev;
564
565         pr_info("Registering PCC driver as Mailbox controller\n");
566         ret = mbox_controller_register(&pcc_mbox_ctrl);
567
568         if (ret) {
569                 pr_err("Err registering PCC as Mailbox controller: %d\n", ret);
570                 ret = -ENODEV;
571         }
572
573         return ret;
574 }
575
576 static struct platform_driver pcc_mbox_driver = {
577         .probe = pcc_mbox_probe,
578         .driver = {
579                 .name = "PCCT",
580         },
581 };
582
583 static int __init pcc_init(void)
584 {
585         int ret;
586         struct platform_device *pcc_pdev;
587
588         if (acpi_disabled)
589                 return -ENODEV;
590
591         /* Check if PCC support is available. */
592         ret = acpi_pcc_probe();
593
594         if (ret) {
595                 pr_debug("ACPI PCC probe failed.\n");
596                 return -ENODEV;
597         }
598
599         pcc_pdev = platform_create_bundle(&pcc_mbox_driver,
600                         pcc_mbox_probe, NULL, 0, NULL, 0);
601
602         if (IS_ERR(pcc_pdev)) {
603                 pr_debug("Err creating PCC platform bundle\n");
604                 return PTR_ERR(pcc_pdev);
605         }
606
607         return 0;
608 }
609
610 /*
611  * Make PCC init postcore so that users of this mailbox
612  * such as the ACPI Processor driver have it available
613  * at their init.
614  */
615 postcore_initcall(pcc_init);