blob: 23391e224a68d158905f120786f7b2a55578ff84 [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
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -070067/* Array of cached virtual address for doorbell registers */
68static void __iomem **pcc_doorbell_vaddr;
hotranaca314e2016-08-15 17:14:05 -070069/* Array of cached virtual address for doorbell ack registers */
70static void __iomem **pcc_doorbell_ack_vaddr;
71/* Array of doorbell interrupts */
72static int *pcc_doorbell_irq;
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -070073
Ashwin Chaugule86c22f82014-11-12 19:59:38 -050074static 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 */
83static struct mbox_chan *get_pcc_channel(int id)
84{
Hoan Tranecfc1592017-07-21 15:09:29 -070085 if (id < 0 || id >= pcc_mbox_ctrl.num_chans)
Ashwin Chaugule86c22f82014-11-12 19:59:38 -050086 return ERR_PTR(-ENOENT);
87
Alexey Klimove9c8dc82015-12-10 17:28:37 +000088 return &pcc_mbox_channels[id];
Ashwin Chaugule86c22f82014-11-12 19:59:38 -050089}
90
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -070091/*
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 */
99static 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
125static 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 Chaugule86c22f82014-11-12 19:59:38 -0500151/**
hotranaca314e2016-08-15 17:14:05 -0700152 * 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 */
159static 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 Holla10dcc2d2021-09-17 14:33:44 +0100177 * @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
hotranaca314e2016-08-15 17:14:05 -0700181 */
182static 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. Boxc7a1dfb2017-06-05 16:39:08 +0800200 doorbell_ack = &pcct2_ss->platform_ack_register;
hotranaca314e2016-08-15 17:14:05 -0700201 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 */
234struct 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 Holla33cd7122017-09-28 11:18:52 +0100263 chan->txdone_method = TXDONE_BY_ACK;
hotranaca314e2016-08-15 17:14:05 -0700264
Hoan Tran6ca595a2016-11-14 11:19:02 -0800265 spin_unlock_irqrestore(&chan->lock, flags);
266
hotranaca314e2016-08-15 17:14:05 -0700267 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 Tran6ca595a2016-11-14 11:19:02 -0800275 pcc_mbox_free_channel(chan);
hotranaca314e2016-08-15 17:14:05 -0700276 chan = ERR_PTR(rc);
277 }
278 }
279
hotranaca314e2016-08-15 17:14:05 -0700280 return chan;
281}
282EXPORT_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 */
290void 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 Tran6ca595a2016-11-14 11:19:02 -0800303 if (pcc_doorbell_irq[id] > 0)
304 devm_free_irq(chan->mbox->dev, pcc_doorbell_irq[id], chan);
305
hotranaca314e2016-08-15 17:14:05 -0700306 spin_lock_irqsave(&chan->lock, flags);
307 chan->cl = NULL;
308 chan->active_req = NULL;
Sudeep Holla33cd7122017-09-28 11:18:52 +0100309 if (chan->txdone_method == TXDONE_BY_ACK)
hotranaca314e2016-08-15 17:14:05 -0700310 chan->txdone_method = TXDONE_BY_POLL;
311
hotranaca314e2016-08-15 17:14:05 -0700312 spin_unlock_irqrestore(&chan->lock, flags);
313}
314EXPORT_SYMBOL_GPL(pcc_mbox_free_channel);
315
hotranaca314e2016-08-15 17:14:05 -0700316/**
Ashwin Chaugule33350e62015-01-27 16:03:57 -0500317 * 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 Chaugule86c22f82014-11-12 19:59:38 -0500322 * @chan: Pointer to Mailbox channel over which to send data.
Ashwin Chaugule33350e62015-01-27 16:03:57 -0500323 * @data: Client specific data written over channel. Used here
324 * only for debug after PCC transaction completes.
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500325 *
326 * Return: Err if something failed else 0 for success.
327 */
328static int pcc_send_data(struct mbox_chan *chan, void *data)
329{
330 struct acpi_pcct_hw_reduced *pcct_ss = chan->con_priv;
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700331 struct acpi_generic_address *doorbell;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500332 u64 doorbell_preserve;
333 u64 doorbell_val;
334 u64 doorbell_write;
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700335 u32 id = chan - pcc_mbox_channels;
336 int ret = 0;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500337
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700338 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 Chaugule86c22f82014-11-12 19:59:38 -0500344 doorbell_preserve = pcct_ss->preserve_mask;
345 doorbell_write = pcct_ss->write_mask;
346
Ashwin Chaugule33350e62015-01-27 16:03:57 -0500347 /* Sync notification from OS to Platform. */
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700348 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 Chaugule86c22f82014-11-12 19:59:38 -0500364}
365
Andrew Bresticker05ae7972015-05-04 10:36:35 -0700366static const struct mbox_chan_ops pcc_chan_ops = {
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500367 .send_data = pcc_send_data,
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500368};
369
370/**
Sudeep Holla10dcc2d2021-09-17 14:33:44 +0100371 * parse_pcc_subspace - Count PCC subspaces defined
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500372 * @header: Pointer to the ACPI subtable header under the PCCT.
373 * @end: End of subtable entry.
374 *
Al Stone8f8027c2018-05-16 16:01:41 -0600375 * Return: If we find a PCC subspace entry of a valid type, return 0.
376 * Otherwise, return -EINVAL.
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500377 *
378 * This gets called for each entry in the PCC table.
379 */
Keith Busch60574d12019-03-11 14:55:57 -0600380static int parse_pcc_subspace(union acpi_subtable_headers *header,
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500381 const unsigned long end)
382{
Al Stone8f8027c2018-05-16 16:01:41 -0600383 struct acpi_pcct_subspace *ss = (struct acpi_pcct_subspace *) header;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500384
Al Stone8f8027c2018-05-16 16:01:41 -0600385 if (ss->header.type < ACPI_PCCT_TYPE_RESERVED)
386 return 0;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500387
Al Stone8f8027c2018-05-16 16:01:41 -0600388 return -EINVAL;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500389}
390
391/**
hotranaca314e2016-08-15 17:14:05 -0700392 * 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 */
401static int pcc_parse_subspace_irq(int id,
402 struct acpi_pcct_hw_reduced *pcct_ss)
403{
David E. Boxc7a1dfb2017-06-05 16:39:08 +0800404 pcc_doorbell_irq[id] = pcc_map_interrupt(pcct_ss->platform_interrupt,
hotranaca314e2016-08-15 17:14:05 -0700405 (u32)pcct_ss->flags);
406 if (pcc_doorbell_irq[id] <= 0) {
407 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
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. Boxc7a1dfb2017-06-05 16:39:08 +0800417 pcct2_ss->platform_ack_register.address,
418 pcct2_ss->platform_ack_register.bit_width / 8);
hotranaca314e2016-08-15 17:14:05 -0700419 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 Chaugule86c22f82014-11-12 19:59:38 -0500429 * acpi_pcc_probe - Parse the ACPI tree for the PCCT.
430 *
431 * Return: 0 for Success, else errno.
432 */
433static int __init acpi_pcc_probe(void)
434{
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500435 struct acpi_table_header *pcct_tbl;
436 struct acpi_subtable_header *pcct_entry;
hotranaca314e2016-08-15 17:14:05 -0700437 struct acpi_table_pcct *acpi_pcct_tbl;
Al Stone8f8027c2018-05-16 16:01:41 -0600438 struct acpi_subtable_proc proc[ACPI_PCCT_TYPE_RESERVED];
hotranaca314e2016-08-15 17:14:05 -0700439 int count, i, rc;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500440 acpi_status status = AE_OK;
441
442 /* Search for PCCT */
Lv Zheng6b11d1d2016-12-14 15:04:39 +0800443 status = acpi_get_table(ACPI_SIG_PCCT, 0, &pcct_tbl);
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500444
Punit Agrawal66ed4ca2017-08-01 13:43:57 +0100445 if (ACPI_FAILURE(status) || !pcct_tbl)
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500446 return -ENODEV;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500447
Al Stone8f8027c2018-05-16 16:01:41 -0600448 /* 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 Chaugule86c22f82014-11-12 19:59:38 -0500455
Al Stone8f8027c2018-05-16 16:01:41 -0600456 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 Arcariafd0b1f2018-08-27 15:19:08 -0400459 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 Guo425ab032020-07-22 17:40:40 +0800464
465 rc = -EINVAL;
466 goto err_put_pcct;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500467 }
468
Kees Cook6396bb22018-06-12 14:03:40 -0700469 pcc_mbox_channels = kcalloc(count, sizeof(struct mbox_chan),
470 GFP_KERNEL);
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500471 if (!pcc_mbox_channels) {
472 pr_err("Could not allocate space for PCC mbox channels\n");
Hanjun Guo425ab032020-07-22 17:40:40 +0800473 rc = -ENOMEM;
474 goto err_put_pcct;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500475 }
476
Al Stone8f8027c2018-05-16 16:01:41 -0600477 pcc_doorbell_vaddr = kcalloc(count, sizeof(void *), GFP_KERNEL);
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700478 if (!pcc_doorbell_vaddr) {
hotranaca314e2016-08-15 17:14:05 -0700479 rc = -ENOMEM;
480 goto err_free_mbox;
481 }
482
Al Stone8f8027c2018-05-16 16:01:41 -0600483 pcc_doorbell_ack_vaddr = kcalloc(count, sizeof(void *), GFP_KERNEL);
hotranaca314e2016-08-15 17:14:05 -0700484 if (!pcc_doorbell_ack_vaddr) {
485 rc = -ENOMEM;
486 goto err_free_db_vaddr;
487 }
488
Al Stone8f8027c2018-05-16 16:01:41 -0600489 pcc_doorbell_irq = kcalloc(count, sizeof(int), GFP_KERNEL);
hotranaca314e2016-08-15 17:14:05 -0700490 if (!pcc_doorbell_irq) {
491 rc = -ENOMEM;
492 goto err_free_db_ack_vaddr;
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700493 }
494
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500495 /* 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
hotranaca314e2016-08-15 17:14:05 -0700499 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 Stone8f8027c2018-05-16 16:01:41 -0600503 for (i = 0; i < count; i++) {
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700504 struct acpi_generic_address *db_reg;
Al Stone8f8027c2018-05-16 16:01:41 -0600505 struct acpi_pcct_subspace *pcct_ss;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500506 pcc_mbox_channels[i].con_priv = pcct_entry;
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700507
Al Stone8f8027c2018-05-16 16:01:41 -0600508 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;
hotranaca314e2016-08-15 17:14:05 -0700511
Al Stone8f8027c2018-05-16 16:01:41 -0600512 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 }
hotranaca314e2016-08-15 17:14:05 -0700519 }
Al Stone8f8027c2018-05-16 16:01:41 -0600520 pcct_ss = (struct acpi_pcct_subspace *) pcct_entry;
hotranaca314e2016-08-15 17:14:05 -0700521
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700522 /* If doorbell is in system memory cache the virt address */
Prakash, Prashanth8b0f5782016-02-17 13:21:01 -0700523 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 Chaugule86c22f82014-11-12 19:59:38 -0500527 pcct_entry = (struct acpi_subtable_header *)
528 ((unsigned long) pcct_entry + pcct_entry->length);
529 }
530
Al Stone8f8027c2018-05-16 16:01:41 -0600531 pcc_mbox_ctrl.num_chans = count;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500532
533 pr_info("Detected %d PCC Subspaces\n", pcc_mbox_ctrl.num_chans);
534
535 return 0;
hotranaca314e2016-08-15 17:14:05 -0700536
537err:
538 kfree(pcc_doorbell_irq);
539err_free_db_ack_vaddr:
540 kfree(pcc_doorbell_ack_vaddr);
541err_free_db_vaddr:
542 kfree(pcc_doorbell_vaddr);
543err_free_mbox:
544 kfree(pcc_mbox_channels);
Hanjun Guo425ab032020-07-22 17:40:40 +0800545err_put_pcct:
546 acpi_put_table(pcct_tbl);
hotranaca314e2016-08-15 17:14:05 -0700547 return rc;
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500548}
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 */
561static 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 Chaugule86c22f82014-11-12 19:59:38 -0500567 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 Yan00d99902020-04-03 11:52:08 +0800580static struct platform_driver pcc_mbox_driver = {
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500581 .probe = pcc_mbox_probe,
582 .driver = {
583 .name = "PCCT",
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500584 },
585};
586
587static 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. Wysockiefd756d2015-02-05 00:40:08 +0100599 pr_debug("ACPI PCC probe failed.\n");
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500600 return -ENODEV;
601 }
602
603 pcc_pdev = platform_create_bundle(&pcc_mbox_driver,
604 pcc_mbox_probe, NULL, 0, NULL, 0);
605
Wei Yongjun356d5d22015-01-14 09:10:56 +0800606 if (IS_ERR(pcc_pdev)) {
Rafael J. Wysockiefd756d2015-02-05 00:40:08 +0100607 pr_debug("Err creating PCC platform bundle\n");
Wei Yongjun356d5d22015-01-14 09:10:56 +0800608 return PTR_ERR(pcc_pdev);
Ashwin Chaugule86c22f82014-11-12 19:59:38 -0500609 }
610
611 return 0;
612}
Ashwin Chauguled3c68f22015-08-05 09:40:24 -0400613
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 */
619postcore_initcall(pcc_init);