blob: 4bace1fa48f0b117dc99bdf035ac2d9f11f6a234 [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>
Sudeep Holla800cda72021-09-17 14:33:52 +010055#include <linux/log2.h>
Ashwin Chaugule86c22f82014-11-12 19:59:38 -050056#include <linux/platform_device.h>
57#include <linux/mailbox_controller.h>
58#include <linux/mailbox_client.h>
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -070059#include <linux/io-64-nonatomic-lo-hi.h>
Hoan Tran6ca595a2016-11-14 11:19:02 -080060#include <acpi/pcc.h>
Ashwin Chaugule86c22f82014-11-12 19:59:38 -050061
62#include "mailbox.h"
63
hotranaca314e2016-08-15 17:14:05 -070064#define MBOX_IRQ_NAME "pcc-mbox"
Ashwin Chaugule86c22f82014-11-12 19:59:38 -050065
66static struct mbox_chan *pcc_mbox_channels;
67
Sudeep Holla80b2bdd2021-09-17 14:33:46 +010068/**
Sudeep Holla800cda72021-09-17 14:33:52 +010069 * struct pcc_chan_reg - PCC register bundle
70 *
71 * @vaddr: cached virtual address for this register
72 * @gas: pointer to the generic address structure for this register
73 * @preserve_mask: bitmask to preserve when writing to this register
74 * @set_mask: bitmask to set when writing to this register
75 * @status_mask: bitmask to determine and/or update the status for this register
76 */
77struct pcc_chan_reg {
78 void __iomem *vaddr;
79 struct acpi_generic_address *gas;
80 u64 preserve_mask;
81 u64 set_mask;
82 u64 status_mask;
83};
84
85/**
Sudeep Holla80b2bdd2021-09-17 14:33:46 +010086 * struct pcc_chan_info - PCC channel specific information
87 *
Sudeep Holla0f2591e2021-09-17 14:33:49 +010088 * @chan: PCC channel information with Shared Memory Region info
Sudeep Hollabf181232021-09-17 14:33:53 +010089 * @db: PCC register bundle for the doorbell register
90 * @plat_irq_ack: PCC register bundle for the platform interrupt acknowledge
91 * register
Sudeep Hollaf92ae902021-09-17 14:33:51 +010092 * @plat_irq: platform interrupt
Sudeep Holla80b2bdd2021-09-17 14:33:46 +010093 */
94struct pcc_chan_info {
Sudeep Holla0f2591e2021-09-17 14:33:49 +010095 struct pcc_mbox_chan chan;
Sudeep Hollabf181232021-09-17 14:33:53 +010096 struct pcc_chan_reg db;
97 struct pcc_chan_reg plat_irq_ack;
Sudeep Hollaf92ae902021-09-17 14:33:51 +010098 int plat_irq;
Sudeep Holla80b2bdd2021-09-17 14:33:46 +010099};
100
Sudeep Holla7b6da7f2021-09-17 14:33:50 +0100101#define to_pcc_chan_info(c) container_of(c, struct pcc_chan_info, chan)
Sudeep Holla80b2bdd2021-09-17 14:33:46 +0100102static struct pcc_chan_info *chan_info;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500103static struct mbox_controller pcc_mbox_ctrl = {};
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500104
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700105/*
106 * PCC can be used with perf critical drivers such as CPPC
107 * So it makes sense to locally cache the virtual address and
108 * use it to read/write to PCC registers such as doorbell register
109 *
110 * The below read_register and write_registers are used to read and
111 * write from perf critical registers such as PCC doorbell register
112 */
Sudeep Holla45ec2da2021-09-17 14:33:54 +0100113static void read_register(void __iomem *vaddr, u64 *val, unsigned int bit_width)
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700114{
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700115 switch (bit_width) {
116 case 8:
117 *val = readb(vaddr);
118 break;
119 case 16:
120 *val = readw(vaddr);
121 break;
122 case 32:
123 *val = readl(vaddr);
124 break;
125 case 64:
126 *val = readq(vaddr);
127 break;
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700128 }
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700129}
130
Sudeep Holla45ec2da2021-09-17 14:33:54 +0100131static void write_register(void __iomem *vaddr, u64 val, unsigned int bit_width)
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700132{
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700133 switch (bit_width) {
134 case 8:
135 writeb(val, vaddr);
136 break;
137 case 16:
138 writew(val, vaddr);
139 break;
140 case 32:
141 writel(val, vaddr);
142 break;
143 case 64:
144 writeq(val, vaddr);
145 break;
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700146 }
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700147}
148
Sudeep Holla800cda72021-09-17 14:33:52 +0100149static int pcc_chan_reg_read(struct pcc_chan_reg *reg, u64 *val)
150{
151 int ret = 0;
152
153 if (!reg->gas) {
154 *val = 0;
155 return 0;
156 }
157
158 if (reg->vaddr)
Sudeep Holla45ec2da2021-09-17 14:33:54 +0100159 read_register(reg->vaddr, val, reg->gas->bit_width);
Sudeep Holla800cda72021-09-17 14:33:52 +0100160 else
161 ret = acpi_read(val, reg->gas);
162
163 return ret;
164}
165
166static int pcc_chan_reg_write(struct pcc_chan_reg *reg, u64 val)
167{
168 int ret = 0;
169
170 if (!reg->gas)
171 return 0;
172
173 if (reg->vaddr)
Sudeep Holla45ec2da2021-09-17 14:33:54 +0100174 write_register(reg->vaddr, val, reg->gas->bit_width);
Sudeep Holla800cda72021-09-17 14:33:52 +0100175 else
176 ret = acpi_write(val, reg->gas);
177
178 return ret;
179}
180
181static int pcc_chan_reg_read_modify_write(struct pcc_chan_reg *reg)
182{
183 int ret = 0;
184 u64 val;
185
186 ret = pcc_chan_reg_read(reg, &val);
187 if (ret)
188 return ret;
189
190 val &= reg->preserve_mask;
191 val |= reg->set_mask;
192
193 return pcc_chan_reg_write(reg, val);
194}
195
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500196/**
hotranaca314e2016-08-15 17:14:05 -0700197 * pcc_map_interrupt - Map a PCC subspace GSI to a linux IRQ number
198 * @interrupt: GSI number.
199 * @flags: interrupt flags
200 *
201 * Returns: a valid linux IRQ number on success
202 * 0 or -EINVAL on failure
203 */
204static int pcc_map_interrupt(u32 interrupt, u32 flags)
205{
206 int trigger, polarity;
207
208 if (!interrupt)
209 return 0;
210
211 trigger = (flags & ACPI_PCCT_INTERRUPT_MODE) ? ACPI_EDGE_SENSITIVE
212 : ACPI_LEVEL_SENSITIVE;
213
214 polarity = (flags & ACPI_PCCT_INTERRUPT_POLARITY) ? ACPI_ACTIVE_LOW
215 : ACPI_ACTIVE_HIGH;
216
217 return acpi_register_gsi(NULL, interrupt, trigger, polarity);
218}
219
220/**
221 * pcc_mbox_irq - PCC mailbox interrupt handler
Sudeep Holla10dcc2d2021-09-17 14:33:44 +0100222 * @irq: interrupt number
223 * @p: data/cookie passed from the caller to identify the channel
224 *
225 * Returns: IRQ_HANDLED if interrupt is handled or IRQ_NONE if not
hotranaca314e2016-08-15 17:14:05 -0700226 */
227static irqreturn_t pcc_mbox_irq(int irq, void *p)
228{
Sudeep Holla80b2bdd2021-09-17 14:33:46 +0100229 struct pcc_chan_info *pchan;
hotranaca314e2016-08-15 17:14:05 -0700230 struct mbox_chan *chan = p;
hotranaca314e2016-08-15 17:14:05 -0700231
Sudeep Hollabf181232021-09-17 14:33:53 +0100232 pchan = chan->con_priv;
233
234 if (pcc_chan_reg_read_modify_write(&pchan->plat_irq_ack))
235 return IRQ_NONE;
hotranaca314e2016-08-15 17:14:05 -0700236
237 mbox_chan_received_data(chan, NULL);
238
hotranaca314e2016-08-15 17:14:05 -0700239 return IRQ_HANDLED;
240}
241
242/**
243 * pcc_mbox_request_channel - PCC clients call this function to
244 * request a pointer to their PCC subspace, from which they
245 * can get the details of communicating with the remote.
246 * @cl: Pointer to Mailbox client, so we know where to bind the
247 * Channel.
248 * @subspace_id: The PCC Subspace index as parsed in the PCC client
249 * ACPI package. This is used to lookup the array of PCC
250 * subspaces as parsed by the PCC Mailbox controller.
251 *
Sudeep Holla7b6da7f2021-09-17 14:33:50 +0100252 * Return: Pointer to the PCC Mailbox Channel if successful or ERR_PTR.
hotranaca314e2016-08-15 17:14:05 -0700253 */
Sudeep Holla7b6da7f2021-09-17 14:33:50 +0100254struct pcc_mbox_chan *
255pcc_mbox_request_channel(struct mbox_client *cl, int subspace_id)
hotranaca314e2016-08-15 17:14:05 -0700256{
Sudeep Holla80b2bdd2021-09-17 14:33:46 +0100257 struct pcc_chan_info *pchan;
hotranaca314e2016-08-15 17:14:05 -0700258 struct device *dev = pcc_mbox_ctrl.dev;
259 struct mbox_chan *chan;
260 unsigned long flags;
261
Sudeep Holla7b6da7f2021-09-17 14:33:50 +0100262 if (subspace_id < 0 || subspace_id >= pcc_mbox_ctrl.num_chans)
263 return ERR_PTR(-ENOENT);
hotranaca314e2016-08-15 17:14:05 -0700264
Sudeep Holla7b6da7f2021-09-17 14:33:50 +0100265 pchan = chan_info + subspace_id;
266 chan = pchan->chan.mchan;
hotranaca314e2016-08-15 17:14:05 -0700267 if (IS_ERR(chan) || chan->cl) {
268 dev_err(dev, "Channel not found for idx: %d\n", subspace_id);
269 return ERR_PTR(-EBUSY);
270 }
271
272 spin_lock_irqsave(&chan->lock, flags);
273 chan->msg_free = 0;
274 chan->msg_count = 0;
275 chan->active_req = NULL;
276 chan->cl = cl;
277 init_completion(&chan->tx_complete);
278
279 if (chan->txdone_method == TXDONE_BY_POLL && cl->knows_txdone)
Sudeep Holla33cd7122017-09-28 11:18:52 +0100280 chan->txdone_method = TXDONE_BY_ACK;
hotranaca314e2016-08-15 17:14:05 -0700281
Hoan Tran6ca595a2016-11-14 11:19:02 -0800282 spin_unlock_irqrestore(&chan->lock, flags);
283
Sudeep Hollaf92ae902021-09-17 14:33:51 +0100284 if (pchan->plat_irq > 0) {
hotranaca314e2016-08-15 17:14:05 -0700285 int rc;
286
Sudeep Hollaf92ae902021-09-17 14:33:51 +0100287 rc = devm_request_irq(dev, pchan->plat_irq, pcc_mbox_irq, 0,
Sudeep Holla80b2bdd2021-09-17 14:33:46 +0100288 MBOX_IRQ_NAME, chan);
hotranaca314e2016-08-15 17:14:05 -0700289 if (unlikely(rc)) {
290 dev_err(dev, "failed to register PCC interrupt %d\n",
Sudeep Hollaf92ae902021-09-17 14:33:51 +0100291 pchan->plat_irq);
Sudeep Holla7b6da7f2021-09-17 14:33:50 +0100292 pcc_mbox_free_channel(&pchan->chan);
293 return ERR_PTR(rc);
hotranaca314e2016-08-15 17:14:05 -0700294 }
295 }
296
Sudeep Holla7b6da7f2021-09-17 14:33:50 +0100297 return &pchan->chan;
hotranaca314e2016-08-15 17:14:05 -0700298}
299EXPORT_SYMBOL_GPL(pcc_mbox_request_channel);
300
301/**
302 * pcc_mbox_free_channel - Clients call this to free their Channel.
303 *
Sudeep Holla7b6da7f2021-09-17 14:33:50 +0100304 * @pchan: Pointer to the PCC mailbox channel as returned by
305 * pcc_mbox_request_channel()
hotranaca314e2016-08-15 17:14:05 -0700306 */
Sudeep Holla7b6da7f2021-09-17 14:33:50 +0100307void pcc_mbox_free_channel(struct pcc_mbox_chan *pchan)
hotranaca314e2016-08-15 17:14:05 -0700308{
Sudeep Holla7b6da7f2021-09-17 14:33:50 +0100309 struct pcc_chan_info *pchan_info = to_pcc_chan_info(pchan);
310 struct mbox_chan *chan = pchan->mchan;
hotranaca314e2016-08-15 17:14:05 -0700311 unsigned long flags;
312
313 if (!chan || !chan->cl)
314 return;
315
Sudeep Hollaf92ae902021-09-17 14:33:51 +0100316 if (pchan_info->plat_irq > 0)
317 devm_free_irq(chan->mbox->dev, pchan_info->plat_irq, chan);
Hoan Tran6ca595a2016-11-14 11:19:02 -0800318
hotranaca314e2016-08-15 17:14:05 -0700319 spin_lock_irqsave(&chan->lock, flags);
320 chan->cl = NULL;
321 chan->active_req = NULL;
Sudeep Holla33cd7122017-09-28 11:18:52 +0100322 if (chan->txdone_method == TXDONE_BY_ACK)
hotranaca314e2016-08-15 17:14:05 -0700323 chan->txdone_method = TXDONE_BY_POLL;
324
hotranaca314e2016-08-15 17:14:05 -0700325 spin_unlock_irqrestore(&chan->lock, flags);
326}
327EXPORT_SYMBOL_GPL(pcc_mbox_free_channel);
328
hotranaca314e2016-08-15 17:14:05 -0700329/**
Ashwin Chaugule33350e62015-01-27 16:03:57 -0500330 * pcc_send_data - Called from Mailbox Controller code. Used
331 * here only to ring the channel doorbell. The PCC client
332 * specific read/write is done in the client driver in
333 * order to maintain atomicity over PCC channel once
334 * OS has control over it. See above for flow of operations.
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500335 * @chan: Pointer to Mailbox channel over which to send data.
Ashwin Chaugule33350e62015-01-27 16:03:57 -0500336 * @data: Client specific data written over channel. Used here
337 * only for debug after PCC transaction completes.
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500338 *
339 * Return: Err if something failed else 0 for success.
340 */
341static int pcc_send_data(struct mbox_chan *chan, void *data)
342{
Sudeep Hollabf181232021-09-17 14:33:53 +0100343 struct pcc_chan_info *pchan = chan->con_priv;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500344
Sudeep Hollabf181232021-09-17 14:33:53 +0100345 return pcc_chan_reg_read_modify_write(&pchan->db);
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500346}
347
Andrew Bresticker05ae7972015-05-04 10:36:35 -0700348static const struct mbox_chan_ops pcc_chan_ops = {
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500349 .send_data = pcc_send_data,
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500350};
351
352/**
Sudeep Holla10dcc2d2021-09-17 14:33:44 +0100353 * parse_pcc_subspace - Count PCC subspaces defined
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500354 * @header: Pointer to the ACPI subtable header under the PCCT.
355 * @end: End of subtable entry.
356 *
Al Stone8f8027c2018-05-16 16:01:41 -0600357 * Return: If we find a PCC subspace entry of a valid type, return 0.
358 * Otherwise, return -EINVAL.
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500359 *
360 * This gets called for each entry in the PCC table.
361 */
Keith Busch60574d12019-03-11 14:55:57 -0600362static int parse_pcc_subspace(union acpi_subtable_headers *header,
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500363 const unsigned long end)
364{
Al Stone8f8027c2018-05-16 16:01:41 -0600365 struct acpi_pcct_subspace *ss = (struct acpi_pcct_subspace *) header;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500366
Al Stone8f8027c2018-05-16 16:01:41 -0600367 if (ss->header.type < ACPI_PCCT_TYPE_RESERVED)
368 return 0;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500369
Al Stone8f8027c2018-05-16 16:01:41 -0600370 return -EINVAL;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500371}
372
Sudeep Holla800cda72021-09-17 14:33:52 +0100373static int
374pcc_chan_reg_init(struct pcc_chan_reg *reg, struct acpi_generic_address *gas,
375 u64 preserve_mask, u64 set_mask, u64 status_mask, char *name)
376{
377 if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
378 if (!(gas->bit_width >= 8 && gas->bit_width <= 64 &&
379 is_power_of_2(gas->bit_width))) {
380 pr_err("Error: Cannot access register of %u bit width",
381 gas->bit_width);
382 return -EFAULT;
383 }
384
385 reg->vaddr = acpi_os_ioremap(gas->address, gas->bit_width / 8);
386 if (!reg->vaddr) {
387 pr_err("Failed to ioremap PCC %s register\n", name);
388 return -ENOMEM;
389 }
390 }
391 reg->gas = gas;
392 reg->preserve_mask = preserve_mask;
393 reg->set_mask = set_mask;
394 reg->status_mask = status_mask;
395 return 0;
396}
397
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500398/**
hotranaca314e2016-08-15 17:14:05 -0700399 * pcc_parse_subspace_irq - Parse the PCC IRQ and PCC ACK register
Sudeep Holla319bfb32021-09-17 14:33:47 +0100400 *
401 * @pchan: Pointer to the PCC channel info structure.
402 * @pcct_entry: Pointer to the ACPI subtable header.
hotranaca314e2016-08-15 17:14:05 -0700403 *
404 * Return: 0 for Success, else errno.
405 *
Sudeep Holla319bfb32021-09-17 14:33:47 +0100406 * There should be one entry per PCC channel. This gets called for each
407 * entry in the PCC table. This uses PCCY Type1 structure for all applicable
408 * types(Type 1-4) to fetch irq
hotranaca314e2016-08-15 17:14:05 -0700409 */
Sudeep Holla319bfb32021-09-17 14:33:47 +0100410static int pcc_parse_subspace_irq(struct pcc_chan_info *pchan,
411 struct acpi_subtable_header *pcct_entry)
hotranaca314e2016-08-15 17:14:05 -0700412{
Sudeep Hollabf181232021-09-17 14:33:53 +0100413 int ret = 0;
Sudeep Holla319bfb32021-09-17 14:33:47 +0100414 struct acpi_pcct_hw_reduced *pcct_ss;
Sudeep Holla80b2bdd2021-09-17 14:33:46 +0100415
Sudeep Holla319bfb32021-09-17 14:33:47 +0100416 if (pcct_entry->type < ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE ||
417 pcct_entry->type > ACPI_PCCT_TYPE_EXT_PCC_SLAVE_SUBSPACE)
418 return 0;
419
420 pcct_ss = (struct acpi_pcct_hw_reduced *)pcct_entry;
Sudeep Hollaf92ae902021-09-17 14:33:51 +0100421 pchan->plat_irq = pcc_map_interrupt(pcct_ss->platform_interrupt,
422 (u32)pcct_ss->flags);
423 if (pchan->plat_irq <= 0) {
hotranaca314e2016-08-15 17:14:05 -0700424 pr_err("PCC GSI %d not registered\n",
David E. Boxc7a1dfb2017-06-05 16:39:08 +0800425 pcct_ss->platform_interrupt);
hotranaca314e2016-08-15 17:14:05 -0700426 return -EINVAL;
427 }
428
Sudeep Holla319bfb32021-09-17 14:33:47 +0100429 if (pcct_ss->header.type == ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE_TYPE2) {
hotranaca314e2016-08-15 17:14:05 -0700430 struct acpi_pcct_hw_reduced_type2 *pcct2_ss = (void *)pcct_ss;
431
Sudeep Hollabf181232021-09-17 14:33:53 +0100432 ret = pcc_chan_reg_init(&pchan->plat_irq_ack,
433 &pcct2_ss->platform_ack_register,
434 pcct2_ss->ack_preserve_mask,
435 pcct2_ss->ack_write_mask, 0,
436 "PLAT IRQ ACK");
hotranaca314e2016-08-15 17:14:05 -0700437 }
438
Sudeep Hollabf181232021-09-17 14:33:53 +0100439 return ret;
hotranaca314e2016-08-15 17:14:05 -0700440}
441
442/**
Sudeep Holla4e3c96f2021-09-17 14:33:48 +0100443 * pcc_parse_subspace_db_reg - Parse the PCC doorbell register
444 *
445 * @pchan: Pointer to the PCC channel info structure.
446 * @pcct_entry: Pointer to the ACPI subtable header.
447 *
Sudeep Hollabf181232021-09-17 14:33:53 +0100448 * Return: 0 for Success, else errno.
Sudeep Holla4e3c96f2021-09-17 14:33:48 +0100449 */
Sudeep Hollabf181232021-09-17 14:33:53 +0100450static int pcc_parse_subspace_db_reg(struct pcc_chan_info *pchan,
451 struct acpi_subtable_header *pcct_entry)
Sudeep Holla4e3c96f2021-09-17 14:33:48 +0100452{
Sudeep Hollabf181232021-09-17 14:33:53 +0100453 int ret = 0;
454
Sudeep Holla4e3c96f2021-09-17 14:33:48 +0100455 struct acpi_pcct_subspace *pcct_ss;
Sudeep Holla4e3c96f2021-09-17 14:33:48 +0100456
457 pcct_ss = (struct acpi_pcct_subspace *)pcct_entry;
458
Sudeep Hollabf181232021-09-17 14:33:53 +0100459 ret = pcc_chan_reg_init(&pchan->db,
460 &pcct_ss->doorbell_register,
461 pcct_ss->preserve_mask,
462 pcct_ss->write_mask, 0, "Doorbell");
463 return ret;
Sudeep Holla4e3c96f2021-09-17 14:33:48 +0100464}
465
466/**
Sudeep Holla0f2591e2021-09-17 14:33:49 +0100467 * pcc_parse_subspace_shmem - Parse the PCC Shared Memory Region information
468 *
469 * @pchan: Pointer to the PCC channel info structure.
470 * @pcct_entry: Pointer to the ACPI subtable header.
471 *
472 */
473static void pcc_parse_subspace_shmem(struct pcc_chan_info *pchan,
474 struct acpi_subtable_header *pcct_entry)
475{
476 struct acpi_pcct_subspace *pcct_ss;
477
478 pcct_ss = (struct acpi_pcct_subspace *)pcct_entry;
479
480 pchan->chan.shmem_base_addr = pcct_ss->base_address;
481 pchan->chan.shmem_size = pcct_ss->length;
482 pchan->chan.latency = pcct_ss->latency;
483 pchan->chan.max_access_rate = pcct_ss->max_access_rate;
484 pchan->chan.min_turnaround_time = pcct_ss->min_turnaround_time;
485}
486
487/**
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500488 * acpi_pcc_probe - Parse the ACPI tree for the PCCT.
489 *
490 * Return: 0 for Success, else errno.
491 */
492static int __init acpi_pcc_probe(void)
493{
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500494 struct acpi_table_header *pcct_tbl;
495 struct acpi_subtable_header *pcct_entry;
hotranaca314e2016-08-15 17:14:05 -0700496 struct acpi_table_pcct *acpi_pcct_tbl;
Al Stone8f8027c2018-05-16 16:01:41 -0600497 struct acpi_subtable_proc proc[ACPI_PCCT_TYPE_RESERVED];
hotranaca314e2016-08-15 17:14:05 -0700498 int count, i, rc;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500499 acpi_status status = AE_OK;
500
501 /* Search for PCCT */
Lv Zheng6b11d1d2016-12-14 15:04:39 +0800502 status = acpi_get_table(ACPI_SIG_PCCT, 0, &pcct_tbl);
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500503
Punit Agrawal66ed4ca2017-08-01 13:43:57 +0100504 if (ACPI_FAILURE(status) || !pcct_tbl)
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500505 return -ENODEV;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500506
Al Stone8f8027c2018-05-16 16:01:41 -0600507 /* Set up the subtable handlers */
508 for (i = ACPI_PCCT_TYPE_GENERIC_SUBSPACE;
509 i < ACPI_PCCT_TYPE_RESERVED; i++) {
510 proc[i].id = i;
511 proc[i].count = 0;
512 proc[i].handler = parse_pcc_subspace;
513 }
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500514
Al Stone8f8027c2018-05-16 16:01:41 -0600515 count = acpi_table_parse_entries_array(ACPI_SIG_PCCT,
516 sizeof(struct acpi_table_pcct), proc,
517 ACPI_PCCT_TYPE_RESERVED, MAX_PCC_SUBSPACES);
David Arcariafd0b1f2018-08-27 15:19:08 -0400518 if (count <= 0 || count > MAX_PCC_SUBSPACES) {
519 if (count < 0)
520 pr_warn("Error parsing PCC subspaces from PCCT\n");
521 else
522 pr_warn("Invalid PCCT: %d PCC subspaces\n", count);
Hanjun Guo425ab032020-07-22 17:40:40 +0800523
524 rc = -EINVAL;
525 goto err_put_pcct;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500526 }
527
Kees Cook6396bb22018-06-12 14:03:40 -0700528 pcc_mbox_channels = kcalloc(count, sizeof(struct mbox_chan),
529 GFP_KERNEL);
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500530 if (!pcc_mbox_channels) {
531 pr_err("Could not allocate space for PCC mbox channels\n");
Hanjun Guo425ab032020-07-22 17:40:40 +0800532 rc = -ENOMEM;
533 goto err_put_pcct;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500534 }
535
Sudeep Holla80b2bdd2021-09-17 14:33:46 +0100536 chan_info = kcalloc(count, sizeof(*chan_info), GFP_KERNEL);
537 if (!chan_info) {
hotranaca314e2016-08-15 17:14:05 -0700538 rc = -ENOMEM;
539 goto err_free_mbox;
540 }
541
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500542 /* Point to the first PCC subspace entry */
543 pcct_entry = (struct acpi_subtable_header *) (
544 (unsigned long) pcct_tbl + sizeof(struct acpi_table_pcct));
545
hotranaca314e2016-08-15 17:14:05 -0700546 acpi_pcct_tbl = (struct acpi_table_pcct *) pcct_tbl;
547 if (acpi_pcct_tbl->flags & ACPI_PCCT_DOORBELL)
548 pcc_mbox_ctrl.txdone_irq = true;
549
Al Stone8f8027c2018-05-16 16:01:41 -0600550 for (i = 0; i < count; i++) {
Sudeep Holla80b2bdd2021-09-17 14:33:46 +0100551 struct pcc_chan_info *pchan = chan_info + i;
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700552
Sudeep Hollabf181232021-09-17 14:33:53 +0100553 pcc_mbox_channels[i].con_priv = pchan;
Sudeep Holla0f2591e2021-09-17 14:33:49 +0100554 pchan->chan.mchan = &pcc_mbox_channels[i];
555
Sudeep Holla319bfb32021-09-17 14:33:47 +0100556 if (pcc_mbox_ctrl.txdone_irq) {
557 rc = pcc_parse_subspace_irq(pchan, pcct_entry);
558 if (rc < 0)
559 goto err;
hotranaca314e2016-08-15 17:14:05 -0700560 }
Sudeep Hollabf181232021-09-17 14:33:53 +0100561 rc = pcc_parse_subspace_db_reg(pchan, pcct_entry);
562 if (rc < 0)
563 goto err;
hotranaca314e2016-08-15 17:14:05 -0700564
Sudeep Holla0f2591e2021-09-17 14:33:49 +0100565 pcc_parse_subspace_shmem(pchan, pcct_entry);
566
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500567 pcct_entry = (struct acpi_subtable_header *)
568 ((unsigned long) pcct_entry + pcct_entry->length);
569 }
570
Al Stone8f8027c2018-05-16 16:01:41 -0600571 pcc_mbox_ctrl.num_chans = count;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500572
573 pr_info("Detected %d PCC Subspaces\n", pcc_mbox_ctrl.num_chans);
574
575 return 0;
hotranaca314e2016-08-15 17:14:05 -0700576
577err:
Sudeep Holla80b2bdd2021-09-17 14:33:46 +0100578 kfree(chan_info);
hotranaca314e2016-08-15 17:14:05 -0700579err_free_mbox:
580 kfree(pcc_mbox_channels);
Hanjun Guo425ab032020-07-22 17:40:40 +0800581err_put_pcct:
582 acpi_put_table(pcct_tbl);
hotranaca314e2016-08-15 17:14:05 -0700583 return rc;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500584}
585
586/**
587 * pcc_mbox_probe - Called when we find a match for the
588 * PCCT platform device. This is purely used to represent
589 * the PCCT as a virtual device for registering with the
590 * generic Mailbox framework.
591 *
592 * @pdev: Pointer to platform device returned when a match
593 * is found.
594 *
595 * Return: 0 for Success, else errno.
596 */
597static int pcc_mbox_probe(struct platform_device *pdev)
598{
599 int ret = 0;
600
601 pcc_mbox_ctrl.chans = pcc_mbox_channels;
602 pcc_mbox_ctrl.ops = &pcc_chan_ops;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500603 pcc_mbox_ctrl.dev = &pdev->dev;
604
605 pr_info("Registering PCC driver as Mailbox controller\n");
606 ret = mbox_controller_register(&pcc_mbox_ctrl);
607
608 if (ret) {
609 pr_err("Err registering PCC as Mailbox controller: %d\n", ret);
610 ret = -ENODEV;
611 }
612
613 return ret;
614}
615
Jason Yan00d99902020-04-03 11:52:08 +0800616static struct platform_driver pcc_mbox_driver = {
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500617 .probe = pcc_mbox_probe,
618 .driver = {
619 .name = "PCCT",
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500620 },
621};
622
623static int __init pcc_init(void)
624{
625 int ret;
626 struct platform_device *pcc_pdev;
627
628 if (acpi_disabled)
629 return -ENODEV;
630
631 /* Check if PCC support is available. */
632 ret = acpi_pcc_probe();
633
634 if (ret) {
Rafael J. Wysockiefd756d2015-02-05 00:40:08 +0100635 pr_debug("ACPI PCC probe failed.\n");
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500636 return -ENODEV;
637 }
638
639 pcc_pdev = platform_create_bundle(&pcc_mbox_driver,
640 pcc_mbox_probe, NULL, 0, NULL, 0);
641
Wei Yongjun356d5d22015-01-14 09:10:56 +0800642 if (IS_ERR(pcc_pdev)) {
Rafael J. Wysockiefd756d2015-02-05 00:40:08 +0100643 pr_debug("Err creating PCC platform bundle\n");
Wei Yongjun356d5d22015-01-14 09:10:56 +0800644 return PTR_ERR(pcc_pdev);
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500645 }
646
647 return 0;
648}
Ashwin Chauguled3c68f22015-08-05 09:40:24 -0400649
650/*
651 * Make PCC init postcore so that users of this mailbox
652 * such as the ACPI Processor driver have it available
653 * at their init.
654 */
655postcore_initcall(pcc_init);