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