blob: ae4b7a4b35312f713c2469434d19530c7d91b4a2 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Ashwin Chaugule86c22f82014-11-12 19:59:38 -05002/*
3 * Copyright (C) 2014 Linaro Ltd.
4 * Author: Ashwin Chaugule <ashwin.chaugule@linaro.org>
5 *
Ashwin Chaugule86c22f82014-11-12 19:59:38 -05006 * 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 Chaugule33350e62015-01-27 16:03:57 -050014 * is also specified in each PCC table entry.
Ashwin Chaugule86c22f82014-11-12 19:59:38 -050015 *
Ashwin Chaugule33350e62015-01-27 16:03:57 -050016 * 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 Saeger9d2e8b92021-03-12 19:31:10 -070035 * * If command completes, then writes have succeeded and it can release
Ashwin Chaugule33350e62015-01-27 16:03:57 -050036 * 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 Chaugule86c22f82014-11-12 19:59:38 -050043 * 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>
hotranaca314e2016-08-15 17:14:05 -070053#include <linux/interrupt.h>
Ashwin Chaugule86c22f82014-11-12 19:59:38 -050054#include <linux/list.h>
55#include <linux/platform_device.h>
56#include <linux/mailbox_controller.h>
57#include <linux/mailbox_client.h>
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -070058#include <linux/io-64-nonatomic-lo-hi.h>
Hoan Tran6ca595a2016-11-14 11:19:02 -080059#include <acpi/pcc.h>
Ashwin Chaugule86c22f82014-11-12 19:59:38 -050060
61#include "mailbox.h"
62
hotranaca314e2016-08-15 17:14:05 -070063#define MBOX_IRQ_NAME "pcc-mbox"
Ashwin Chaugule86c22f82014-11-12 19:59:38 -050064
65static struct mbox_chan *pcc_mbox_channels;
66
Sudeep Holla80b2bdd2021-09-17 14:33:46 +010067/**
68 * struct pcc_chan_info - PCC channel specific information
69 *
Sudeep Holla0f2591e2021-09-17 14:33:49 +010070 * @chan: PCC channel information with Shared Memory Region info
Sudeep Holla80b2bdd2021-09-17 14:33:46 +010071 * @db_vaddr: cached virtual address for doorbell register
Sudeep Hollaf92ae902021-09-17 14:33:51 +010072 * @plat_irq_ack_vaddr: cached virtual address for platform interrupt
73 * acknowledge register
74 * @plat_irq: platform interrupt
Sudeep Holla80b2bdd2021-09-17 14:33:46 +010075 */
76struct pcc_chan_info {
Sudeep Holla0f2591e2021-09-17 14:33:49 +010077 struct pcc_mbox_chan chan;
Sudeep Holla80b2bdd2021-09-17 14:33:46 +010078 void __iomem *db_vaddr;
Sudeep Hollaf92ae902021-09-17 14:33:51 +010079 void __iomem *plat_irq_ack_vaddr;
80 int plat_irq;
Sudeep Holla80b2bdd2021-09-17 14:33:46 +010081};
82
Sudeep Holla7b6da7f2021-09-17 14:33:50 +010083#define to_pcc_chan_info(c) container_of(c, struct pcc_chan_info, chan)
Sudeep Holla80b2bdd2021-09-17 14:33:46 +010084static struct pcc_chan_info *chan_info;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -050085static struct mbox_controller pcc_mbox_ctrl = {};
Ashwin Chaugule86c22f82014-11-12 19:59:38 -050086
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -070087/*
88 * PCC can be used with perf critical drivers such as CPPC
89 * So it makes sense to locally cache the virtual address and
90 * use it to read/write to PCC registers such as doorbell register
91 *
92 * The below read_register and write_registers are used to read and
93 * write from perf critical registers such as PCC doorbell register
94 */
95static int read_register(void __iomem *vaddr, u64 *val, unsigned int bit_width)
96{
97 int ret_val = 0;
98
99 switch (bit_width) {
100 case 8:
101 *val = readb(vaddr);
102 break;
103 case 16:
104 *val = readw(vaddr);
105 break;
106 case 32:
107 *val = readl(vaddr);
108 break;
109 case 64:
110 *val = readq(vaddr);
111 break;
112 default:
113 pr_debug("Error: Cannot read register of %u bit width",
114 bit_width);
115 ret_val = -EFAULT;
116 break;
117 }
118 return ret_val;
119}
120
121static int write_register(void __iomem *vaddr, u64 val, unsigned int bit_width)
122{
123 int ret_val = 0;
124
125 switch (bit_width) {
126 case 8:
127 writeb(val, vaddr);
128 break;
129 case 16:
130 writew(val, vaddr);
131 break;
132 case 32:
133 writel(val, vaddr);
134 break;
135 case 64:
136 writeq(val, vaddr);
137 break;
138 default:
139 pr_debug("Error: Cannot write register of %u bit width",
140 bit_width);
141 ret_val = -EFAULT;
142 break;
143 }
144 return ret_val;
145}
146
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500147/**
hotranaca314e2016-08-15 17:14:05 -0700148 * pcc_map_interrupt - Map a PCC subspace GSI to a linux IRQ number
149 * @interrupt: GSI number.
150 * @flags: interrupt flags
151 *
152 * Returns: a valid linux IRQ number on success
153 * 0 or -EINVAL on failure
154 */
155static int pcc_map_interrupt(u32 interrupt, u32 flags)
156{
157 int trigger, polarity;
158
159 if (!interrupt)
160 return 0;
161
162 trigger = (flags & ACPI_PCCT_INTERRUPT_MODE) ? ACPI_EDGE_SENSITIVE
163 : ACPI_LEVEL_SENSITIVE;
164
165 polarity = (flags & ACPI_PCCT_INTERRUPT_POLARITY) ? ACPI_ACTIVE_LOW
166 : ACPI_ACTIVE_HIGH;
167
168 return acpi_register_gsi(NULL, interrupt, trigger, polarity);
169}
170
171/**
172 * pcc_mbox_irq - PCC mailbox interrupt handler
Sudeep Holla10dcc2d2021-09-17 14:33:44 +0100173 * @irq: interrupt number
174 * @p: data/cookie passed from the caller to identify the channel
175 *
176 * Returns: IRQ_HANDLED if interrupt is handled or IRQ_NONE if not
hotranaca314e2016-08-15 17:14:05 -0700177 */
178static 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;
Sudeep Holla80b2bdd2021-09-17 14:33:46 +0100182 struct pcc_chan_info *pchan;
hotranaca314e2016-08-15 17:14:05 -0700183 struct mbox_chan *chan = p;
184 u64 doorbell_ack_preserve;
185 u64 doorbell_ack_write;
186 u64 doorbell_ack_val;
187 int ret;
188
189 pcct_ss = chan->con_priv;
190
191 mbox_chan_received_data(chan, NULL);
192
193 if (pcct_ss->header.type == ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE_TYPE2) {
194 struct acpi_pcct_hw_reduced_type2 *pcct2_ss = chan->con_priv;
195 u32 id = chan - pcc_mbox_channels;
196
Sudeep Holla80b2bdd2021-09-17 14:33:46 +0100197 pchan = chan_info + id;
David E. Boxc7a1dfb2017-06-05 16:39:08 +0800198 doorbell_ack = &pcct2_ss->platform_ack_register;
hotranaca314e2016-08-15 17:14:05 -0700199 doorbell_ack_preserve = pcct2_ss->ack_preserve_mask;
200 doorbell_ack_write = pcct2_ss->ack_write_mask;
201
Sudeep Hollaf92ae902021-09-17 14:33:51 +0100202 ret = read_register(pchan->plat_irq_ack_vaddr,
Sudeep Holla80b2bdd2021-09-17 14:33:46 +0100203 &doorbell_ack_val, doorbell_ack->bit_width);
hotranaca314e2016-08-15 17:14:05 -0700204 if (ret)
205 return IRQ_NONE;
206
Sudeep Hollaf92ae902021-09-17 14:33:51 +0100207 ret = write_register(pchan->plat_irq_ack_vaddr,
hotranaca314e2016-08-15 17:14:05 -0700208 (doorbell_ack_val & doorbell_ack_preserve)
209 | doorbell_ack_write,
210 doorbell_ack->bit_width);
211 if (ret)
212 return IRQ_NONE;
213 }
214
215 return IRQ_HANDLED;
216}
217
218/**
219 * pcc_mbox_request_channel - PCC clients call this function to
220 * request a pointer to their PCC subspace, from which they
221 * can get the details of communicating with the remote.
222 * @cl: Pointer to Mailbox client, so we know where to bind the
223 * Channel.
224 * @subspace_id: The PCC Subspace index as parsed in the PCC client
225 * ACPI package. This is used to lookup the array of PCC
226 * subspaces as parsed by the PCC Mailbox controller.
227 *
Sudeep Holla7b6da7f2021-09-17 14:33:50 +0100228 * Return: Pointer to the PCC Mailbox Channel if successful or ERR_PTR.
hotranaca314e2016-08-15 17:14:05 -0700229 */
Sudeep Holla7b6da7f2021-09-17 14:33:50 +0100230struct pcc_mbox_chan *
231pcc_mbox_request_channel(struct mbox_client *cl, int subspace_id)
hotranaca314e2016-08-15 17:14:05 -0700232{
Sudeep Holla80b2bdd2021-09-17 14:33:46 +0100233 struct pcc_chan_info *pchan;
hotranaca314e2016-08-15 17:14:05 -0700234 struct device *dev = pcc_mbox_ctrl.dev;
235 struct mbox_chan *chan;
236 unsigned long flags;
237
Sudeep Holla7b6da7f2021-09-17 14:33:50 +0100238 if (subspace_id < 0 || subspace_id >= pcc_mbox_ctrl.num_chans)
239 return ERR_PTR(-ENOENT);
hotranaca314e2016-08-15 17:14:05 -0700240
Sudeep Holla7b6da7f2021-09-17 14:33:50 +0100241 pchan = chan_info + subspace_id;
242 chan = pchan->chan.mchan;
hotranaca314e2016-08-15 17:14:05 -0700243 if (IS_ERR(chan) || chan->cl) {
244 dev_err(dev, "Channel not found for idx: %d\n", subspace_id);
245 return ERR_PTR(-EBUSY);
246 }
247
248 spin_lock_irqsave(&chan->lock, flags);
249 chan->msg_free = 0;
250 chan->msg_count = 0;
251 chan->active_req = NULL;
252 chan->cl = cl;
253 init_completion(&chan->tx_complete);
254
255 if (chan->txdone_method == TXDONE_BY_POLL && cl->knows_txdone)
Sudeep Holla33cd7122017-09-28 11:18:52 +0100256 chan->txdone_method = TXDONE_BY_ACK;
hotranaca314e2016-08-15 17:14:05 -0700257
Hoan Tran6ca595a2016-11-14 11:19:02 -0800258 spin_unlock_irqrestore(&chan->lock, flags);
259
Sudeep Hollaf92ae902021-09-17 14:33:51 +0100260 if (pchan->plat_irq > 0) {
hotranaca314e2016-08-15 17:14:05 -0700261 int rc;
262
Sudeep Hollaf92ae902021-09-17 14:33:51 +0100263 rc = devm_request_irq(dev, pchan->plat_irq, pcc_mbox_irq, 0,
Sudeep Holla80b2bdd2021-09-17 14:33:46 +0100264 MBOX_IRQ_NAME, chan);
hotranaca314e2016-08-15 17:14:05 -0700265 if (unlikely(rc)) {
266 dev_err(dev, "failed to register PCC interrupt %d\n",
Sudeep Hollaf92ae902021-09-17 14:33:51 +0100267 pchan->plat_irq);
Sudeep Holla7b6da7f2021-09-17 14:33:50 +0100268 pcc_mbox_free_channel(&pchan->chan);
269 return ERR_PTR(rc);
hotranaca314e2016-08-15 17:14:05 -0700270 }
271 }
272
Sudeep Holla7b6da7f2021-09-17 14:33:50 +0100273 return &pchan->chan;
hotranaca314e2016-08-15 17:14:05 -0700274}
275EXPORT_SYMBOL_GPL(pcc_mbox_request_channel);
276
277/**
278 * pcc_mbox_free_channel - Clients call this to free their Channel.
279 *
Sudeep Holla7b6da7f2021-09-17 14:33:50 +0100280 * @pchan: Pointer to the PCC mailbox channel as returned by
281 * pcc_mbox_request_channel()
hotranaca314e2016-08-15 17:14:05 -0700282 */
Sudeep Holla7b6da7f2021-09-17 14:33:50 +0100283void pcc_mbox_free_channel(struct pcc_mbox_chan *pchan)
hotranaca314e2016-08-15 17:14:05 -0700284{
Sudeep Holla7b6da7f2021-09-17 14:33:50 +0100285 struct pcc_chan_info *pchan_info = to_pcc_chan_info(pchan);
286 struct mbox_chan *chan = pchan->mchan;
hotranaca314e2016-08-15 17:14:05 -0700287 unsigned long flags;
288
289 if (!chan || !chan->cl)
290 return;
291
Sudeep Hollaf92ae902021-09-17 14:33:51 +0100292 if (pchan_info->plat_irq > 0)
293 devm_free_irq(chan->mbox->dev, pchan_info->plat_irq, chan);
Hoan Tran6ca595a2016-11-14 11:19:02 -0800294
hotranaca314e2016-08-15 17:14:05 -0700295 spin_lock_irqsave(&chan->lock, flags);
296 chan->cl = NULL;
297 chan->active_req = NULL;
Sudeep Holla33cd7122017-09-28 11:18:52 +0100298 if (chan->txdone_method == TXDONE_BY_ACK)
hotranaca314e2016-08-15 17:14:05 -0700299 chan->txdone_method = TXDONE_BY_POLL;
300
hotranaca314e2016-08-15 17:14:05 -0700301 spin_unlock_irqrestore(&chan->lock, flags);
302}
303EXPORT_SYMBOL_GPL(pcc_mbox_free_channel);
304
hotranaca314e2016-08-15 17:14:05 -0700305/**
Ashwin Chaugule33350e62015-01-27 16:03:57 -0500306 * pcc_send_data - Called from Mailbox Controller code. Used
307 * here only to ring the channel doorbell. The PCC client
308 * specific read/write is done in the client driver in
309 * order to maintain atomicity over PCC channel once
310 * OS has control over it. See above for flow of operations.
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500311 * @chan: Pointer to Mailbox channel over which to send data.
Ashwin Chaugule33350e62015-01-27 16:03:57 -0500312 * @data: Client specific data written over channel. Used here
313 * only for debug after PCC transaction completes.
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500314 *
315 * Return: Err if something failed else 0 for success.
316 */
317static int pcc_send_data(struct mbox_chan *chan, void *data)
318{
319 struct acpi_pcct_hw_reduced *pcct_ss = chan->con_priv;
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700320 struct acpi_generic_address *doorbell;
Sudeep Holla80b2bdd2021-09-17 14:33:46 +0100321 struct pcc_chan_info *pchan;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500322 u64 doorbell_preserve;
323 u64 doorbell_val;
324 u64 doorbell_write;
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700325 u32 id = chan - pcc_mbox_channels;
326 int ret = 0;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500327
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700328 if (id >= pcc_mbox_ctrl.num_chans) {
329 pr_debug("pcc_send_data: Invalid mbox_chan passed\n");
330 return -ENOENT;
331 }
332
Sudeep Holla80b2bdd2021-09-17 14:33:46 +0100333 pchan = chan_info + id;
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700334 doorbell = &pcct_ss->doorbell_register;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500335 doorbell_preserve = pcct_ss->preserve_mask;
336 doorbell_write = pcct_ss->write_mask;
337
Ashwin Chaugule33350e62015-01-27 16:03:57 -0500338 /* Sync notification from OS to Platform. */
Sudeep Holla80b2bdd2021-09-17 14:33:46 +0100339 if (pchan->db_vaddr) {
340 ret = read_register(pchan->db_vaddr, &doorbell_val,
341 doorbell->bit_width);
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700342 if (ret)
343 return ret;
Sudeep Holla80b2bdd2021-09-17 14:33:46 +0100344 ret = write_register(pchan->db_vaddr,
345 (doorbell_val & doorbell_preserve)
346 | doorbell_write, doorbell->bit_width);
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700347 } else {
348 ret = acpi_read(&doorbell_val, doorbell);
349 if (ret)
350 return ret;
351 ret = acpi_write((doorbell_val & doorbell_preserve) | doorbell_write,
352 doorbell);
353 }
354 return ret;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500355}
356
Andrew Bresticker05ae7972015-05-04 10:36:35 -0700357static const struct mbox_chan_ops pcc_chan_ops = {
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500358 .send_data = pcc_send_data,
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500359};
360
361/**
Sudeep Holla10dcc2d2021-09-17 14:33:44 +0100362 * parse_pcc_subspace - Count PCC subspaces defined
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500363 * @header: Pointer to the ACPI subtable header under the PCCT.
364 * @end: End of subtable entry.
365 *
Al Stone8f8027c2018-05-16 16:01:41 -0600366 * Return: If we find a PCC subspace entry of a valid type, return 0.
367 * Otherwise, return -EINVAL.
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500368 *
369 * This gets called for each entry in the PCC table.
370 */
Keith Busch60574d12019-03-11 14:55:57 -0600371static int parse_pcc_subspace(union acpi_subtable_headers *header,
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500372 const unsigned long end)
373{
Al Stone8f8027c2018-05-16 16:01:41 -0600374 struct acpi_pcct_subspace *ss = (struct acpi_pcct_subspace *) header;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500375
Al Stone8f8027c2018-05-16 16:01:41 -0600376 if (ss->header.type < ACPI_PCCT_TYPE_RESERVED)
377 return 0;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500378
Al Stone8f8027c2018-05-16 16:01:41 -0600379 return -EINVAL;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500380}
381
382/**
hotranaca314e2016-08-15 17:14:05 -0700383 * pcc_parse_subspace_irq - Parse the PCC IRQ and PCC ACK register
Sudeep Holla319bfb32021-09-17 14:33:47 +0100384 *
385 * @pchan: Pointer to the PCC channel info structure.
386 * @pcct_entry: Pointer to the ACPI subtable header.
hotranaca314e2016-08-15 17:14:05 -0700387 *
388 * Return: 0 for Success, else errno.
389 *
Sudeep Holla319bfb32021-09-17 14:33:47 +0100390 * There should be one entry per PCC channel. This gets called for each
391 * entry in the PCC table. This uses PCCY Type1 structure for all applicable
392 * types(Type 1-4) to fetch irq
hotranaca314e2016-08-15 17:14:05 -0700393 */
Sudeep Holla319bfb32021-09-17 14:33:47 +0100394static int pcc_parse_subspace_irq(struct pcc_chan_info *pchan,
395 struct acpi_subtable_header *pcct_entry)
hotranaca314e2016-08-15 17:14:05 -0700396{
Sudeep Holla319bfb32021-09-17 14:33:47 +0100397 struct acpi_pcct_hw_reduced *pcct_ss;
Sudeep Holla80b2bdd2021-09-17 14:33:46 +0100398
Sudeep Holla319bfb32021-09-17 14:33:47 +0100399 if (pcct_entry->type < ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE ||
400 pcct_entry->type > ACPI_PCCT_TYPE_EXT_PCC_SLAVE_SUBSPACE)
401 return 0;
402
403 pcct_ss = (struct acpi_pcct_hw_reduced *)pcct_entry;
Sudeep Hollaf92ae902021-09-17 14:33:51 +0100404 pchan->plat_irq = pcc_map_interrupt(pcct_ss->platform_interrupt,
405 (u32)pcct_ss->flags);
406 if (pchan->plat_irq <= 0) {
hotranaca314e2016-08-15 17:14:05 -0700407 pr_err("PCC GSI %d not registered\n",
David E. Boxc7a1dfb2017-06-05 16:39:08 +0800408 pcct_ss->platform_interrupt);
hotranaca314e2016-08-15 17:14:05 -0700409 return -EINVAL;
410 }
411
Sudeep Holla319bfb32021-09-17 14:33:47 +0100412 if (pcct_ss->header.type == ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE_TYPE2) {
hotranaca314e2016-08-15 17:14:05 -0700413 struct acpi_pcct_hw_reduced_type2 *pcct2_ss = (void *)pcct_ss;
414
Sudeep Hollaf92ae902021-09-17 14:33:51 +0100415 pchan->plat_irq_ack_vaddr =
Sudeep Holla80b2bdd2021-09-17 14:33:46 +0100416 acpi_os_ioremap(pcct2_ss->platform_ack_register.address,
417 pcct2_ss->platform_ack_register.bit_width / 8);
Sudeep Hollaf92ae902021-09-17 14:33:51 +0100418 if (!pchan->plat_irq_ack_vaddr) {
hotranaca314e2016-08-15 17:14:05 -0700419 pr_err("Failed to ioremap PCC ACK register\n");
420 return -ENOMEM;
421 }
422 }
423
424 return 0;
425}
426
427/**
Sudeep Holla4e3c96f2021-09-17 14:33:48 +0100428 * pcc_parse_subspace_db_reg - Parse the PCC doorbell register
429 *
430 * @pchan: Pointer to the PCC channel info structure.
431 * @pcct_entry: Pointer to the ACPI subtable header.
432 *
433 */
434static void pcc_parse_subspace_db_reg(struct pcc_chan_info *pchan,
435 struct acpi_subtable_header *pcct_entry)
436{
437 struct acpi_pcct_subspace *pcct_ss;
438 struct acpi_generic_address *db_reg;
439
440 pcct_ss = (struct acpi_pcct_subspace *)pcct_entry;
441
442 /* If doorbell is in system memory cache the virt address */
443 db_reg = &pcct_ss->doorbell_register;
444 if (db_reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
445 pchan->db_vaddr = acpi_os_ioremap(db_reg->address,
446 db_reg->bit_width / 8);
447}
448
449/**
Sudeep Holla0f2591e2021-09-17 14:33:49 +0100450 * pcc_parse_subspace_shmem - Parse the PCC Shared Memory Region information
451 *
452 * @pchan: Pointer to the PCC channel info structure.
453 * @pcct_entry: Pointer to the ACPI subtable header.
454 *
455 */
456static void pcc_parse_subspace_shmem(struct pcc_chan_info *pchan,
457 struct acpi_subtable_header *pcct_entry)
458{
459 struct acpi_pcct_subspace *pcct_ss;
460
461 pcct_ss = (struct acpi_pcct_subspace *)pcct_entry;
462
463 pchan->chan.shmem_base_addr = pcct_ss->base_address;
464 pchan->chan.shmem_size = pcct_ss->length;
465 pchan->chan.latency = pcct_ss->latency;
466 pchan->chan.max_access_rate = pcct_ss->max_access_rate;
467 pchan->chan.min_turnaround_time = pcct_ss->min_turnaround_time;
468}
469
470/**
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500471 * acpi_pcc_probe - Parse the ACPI tree for the PCCT.
472 *
473 * Return: 0 for Success, else errno.
474 */
475static int __init acpi_pcc_probe(void)
476{
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500477 struct acpi_table_header *pcct_tbl;
478 struct acpi_subtable_header *pcct_entry;
hotranaca314e2016-08-15 17:14:05 -0700479 struct acpi_table_pcct *acpi_pcct_tbl;
Al Stone8f8027c2018-05-16 16:01:41 -0600480 struct acpi_subtable_proc proc[ACPI_PCCT_TYPE_RESERVED];
hotranaca314e2016-08-15 17:14:05 -0700481 int count, i, rc;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500482 acpi_status status = AE_OK;
483
484 /* Search for PCCT */
Lv Zheng6b11d1d2016-12-14 15:04:39 +0800485 status = acpi_get_table(ACPI_SIG_PCCT, 0, &pcct_tbl);
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500486
Punit Agrawal66ed4ca2017-08-01 13:43:57 +0100487 if (ACPI_FAILURE(status) || !pcct_tbl)
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500488 return -ENODEV;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500489
Al Stone8f8027c2018-05-16 16:01:41 -0600490 /* Set up the subtable handlers */
491 for (i = ACPI_PCCT_TYPE_GENERIC_SUBSPACE;
492 i < ACPI_PCCT_TYPE_RESERVED; i++) {
493 proc[i].id = i;
494 proc[i].count = 0;
495 proc[i].handler = parse_pcc_subspace;
496 }
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500497
Al Stone8f8027c2018-05-16 16:01:41 -0600498 count = acpi_table_parse_entries_array(ACPI_SIG_PCCT,
499 sizeof(struct acpi_table_pcct), proc,
500 ACPI_PCCT_TYPE_RESERVED, MAX_PCC_SUBSPACES);
David Arcariafd0b1f2018-08-27 15:19:08 -0400501 if (count <= 0 || count > MAX_PCC_SUBSPACES) {
502 if (count < 0)
503 pr_warn("Error parsing PCC subspaces from PCCT\n");
504 else
505 pr_warn("Invalid PCCT: %d PCC subspaces\n", count);
Hanjun Guo425ab032020-07-22 17:40:40 +0800506
507 rc = -EINVAL;
508 goto err_put_pcct;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500509 }
510
Kees Cook6396bb22018-06-12 14:03:40 -0700511 pcc_mbox_channels = kcalloc(count, sizeof(struct mbox_chan),
512 GFP_KERNEL);
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500513 if (!pcc_mbox_channels) {
514 pr_err("Could not allocate space for PCC mbox channels\n");
Hanjun Guo425ab032020-07-22 17:40:40 +0800515 rc = -ENOMEM;
516 goto err_put_pcct;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500517 }
518
Sudeep Holla80b2bdd2021-09-17 14:33:46 +0100519 chan_info = kcalloc(count, sizeof(*chan_info), GFP_KERNEL);
520 if (!chan_info) {
hotranaca314e2016-08-15 17:14:05 -0700521 rc = -ENOMEM;
522 goto err_free_mbox;
523 }
524
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500525 /* Point to the first PCC subspace entry */
526 pcct_entry = (struct acpi_subtable_header *) (
527 (unsigned long) pcct_tbl + sizeof(struct acpi_table_pcct));
528
hotranaca314e2016-08-15 17:14:05 -0700529 acpi_pcct_tbl = (struct acpi_table_pcct *) pcct_tbl;
530 if (acpi_pcct_tbl->flags & ACPI_PCCT_DOORBELL)
531 pcc_mbox_ctrl.txdone_irq = true;
532
Al Stone8f8027c2018-05-16 16:01:41 -0600533 for (i = 0; i < count; i++) {
Sudeep Holla80b2bdd2021-09-17 14:33:46 +0100534 struct pcc_chan_info *pchan = chan_info + i;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500535 pcc_mbox_channels[i].con_priv = pcct_entry;
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700536
Sudeep Holla0f2591e2021-09-17 14:33:49 +0100537 pchan->chan.mchan = &pcc_mbox_channels[i];
538
Sudeep Holla319bfb32021-09-17 14:33:47 +0100539 if (pcc_mbox_ctrl.txdone_irq) {
540 rc = pcc_parse_subspace_irq(pchan, pcct_entry);
541 if (rc < 0)
542 goto err;
hotranaca314e2016-08-15 17:14:05 -0700543 }
Sudeep Holla4e3c96f2021-09-17 14:33:48 +0100544 pcc_parse_subspace_db_reg(pchan, pcct_entry);
hotranaca314e2016-08-15 17:14:05 -0700545
Sudeep Holla0f2591e2021-09-17 14:33:49 +0100546 pcc_parse_subspace_shmem(pchan, pcct_entry);
547
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500548 pcct_entry = (struct acpi_subtable_header *)
549 ((unsigned long) pcct_entry + pcct_entry->length);
550 }
551
Al Stone8f8027c2018-05-16 16:01:41 -0600552 pcc_mbox_ctrl.num_chans = count;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500553
554 pr_info("Detected %d PCC Subspaces\n", pcc_mbox_ctrl.num_chans);
555
556 return 0;
hotranaca314e2016-08-15 17:14:05 -0700557
558err:
Sudeep Holla80b2bdd2021-09-17 14:33:46 +0100559 kfree(chan_info);
hotranaca314e2016-08-15 17:14:05 -0700560err_free_mbox:
561 kfree(pcc_mbox_channels);
Hanjun Guo425ab032020-07-22 17:40:40 +0800562err_put_pcct:
563 acpi_put_table(pcct_tbl);
hotranaca314e2016-08-15 17:14:05 -0700564 return rc;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500565}
566
567/**
568 * pcc_mbox_probe - Called when we find a match for the
569 * PCCT platform device. This is purely used to represent
570 * the PCCT as a virtual device for registering with the
571 * generic Mailbox framework.
572 *
573 * @pdev: Pointer to platform device returned when a match
574 * is found.
575 *
576 * Return: 0 for Success, else errno.
577 */
578static int pcc_mbox_probe(struct platform_device *pdev)
579{
580 int ret = 0;
581
582 pcc_mbox_ctrl.chans = pcc_mbox_channels;
583 pcc_mbox_ctrl.ops = &pcc_chan_ops;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500584 pcc_mbox_ctrl.dev = &pdev->dev;
585
586 pr_info("Registering PCC driver as Mailbox controller\n");
587 ret = mbox_controller_register(&pcc_mbox_ctrl);
588
589 if (ret) {
590 pr_err("Err registering PCC as Mailbox controller: %d\n", ret);
591 ret = -ENODEV;
592 }
593
594 return ret;
595}
596
Jason Yan00d99902020-04-03 11:52:08 +0800597static struct platform_driver pcc_mbox_driver = {
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500598 .probe = pcc_mbox_probe,
599 .driver = {
600 .name = "PCCT",
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500601 },
602};
603
604static int __init pcc_init(void)
605{
606 int ret;
607 struct platform_device *pcc_pdev;
608
609 if (acpi_disabled)
610 return -ENODEV;
611
612 /* Check if PCC support is available. */
613 ret = acpi_pcc_probe();
614
615 if (ret) {
Rafael J. Wysockiefd756d2015-02-05 00:40:08 +0100616 pr_debug("ACPI PCC probe failed.\n");
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500617 return -ENODEV;
618 }
619
620 pcc_pdev = platform_create_bundle(&pcc_mbox_driver,
621 pcc_mbox_probe, NULL, 0, NULL, 0);
622
Wei Yongjun356d5d22015-01-14 09:10:56 +0800623 if (IS_ERR(pcc_pdev)) {
Rafael J. Wysockiefd756d2015-02-05 00:40:08 +0100624 pr_debug("Err creating PCC platform bundle\n");
Wei Yongjun356d5d22015-01-14 09:10:56 +0800625 return PTR_ERR(pcc_pdev);
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500626 }
627
628 return 0;
629}
Ashwin Chauguled3c68f22015-08-05 09:40:24 -0400630
631/*
632 * Make PCC init postcore so that users of this mailbox
633 * such as the ACPI Processor driver have it available
634 * at their init.
635 */
636postcore_initcall(pcc_init);