xref: /linux/drivers/mailbox/pcc.c (revision a7cc308da5f78eee8d94bf666c026671a180d7e5)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	Copyright (C) 2014 Linaro Ltd.
4  *	Author:	Ashwin Chaugule <ashwin.chaugule@linaro.org>
5  *
6  *  PCC (Platform Communication Channel) is defined in the ACPI 5.0+
7  *  specification. It is a mailbox like mechanism to allow clients
8  *  such as CPPC (Collaborative Processor Performance Control), RAS
9  *  (Reliability, Availability and Serviceability) and MPST (Memory
10  *  Node Power State Table) to talk to the platform (e.g. BMC) through
11  *  shared memory regions as defined in the PCC table entries. The PCC
12  *  specification supports a Doorbell mechanism for the PCC clients
13  *  to notify the platform about new data. This Doorbell information
14  *  is also specified in each PCC table entry.
15  *
16  *  Typical high level flow of operation is:
17  *
18  *  PCC Reads:
19  *  * Client tries to acquire a channel lock.
20  *  * After it is acquired it writes READ cmd in communication region cmd
21  *		address.
22  *  * Client issues mbox_send_message() which rings the PCC doorbell
23  *		for its PCC channel.
24  *  * If command completes, then client has control over channel and
25  *		it can proceed with its reads.
26  *  * Client releases lock.
27  *
28  *  PCC Writes:
29  *  * Client tries to acquire channel lock.
30  *  * Client writes to its communication region after it acquires a
31  *		channel lock.
32  *  * Client writes WRITE cmd in communication region cmd address.
33  *  * Client issues mbox_send_message() which rings the PCC doorbell
34  *		for its PCC channel.
35  *  * If command completes, then writes have succeeded and it can release
36  *		the channel lock.
37  *
38  *  There is a Nominal latency defined for each channel which indicates
39  *  how long to wait until a command completes. If command is not complete
40  *  the client needs to retry or assume failure.
41  *
42  *	For more details about PCC, please see the ACPI specification from
43  *  http://www.uefi.org/ACPIv5.1 Section 14.
44  *
45  *  This file implements PCC as a Mailbox controller and allows for PCC
46  *  clients to be implemented as its Mailbox Client Channels.
47  */
48 
49 #include <linux/acpi.h>
50 #include <linux/delay.h>
51 #include <linux/io.h>
52 #include <linux/init.h>
53 #include <linux/interrupt.h>
54 #include <linux/list.h>
55 #include <linux/log2.h>
56 #include <linux/platform_device.h>
57 #include <linux/mailbox_controller.h>
58 #include <linux/mailbox_client.h>
59 #include <linux/io-64-nonatomic-lo-hi.h>
60 #include <acpi/pcc.h>
61 
62 #define MBOX_IRQ_NAME		"pcc-mbox"
63 
64 /**
65  * struct pcc_chan_reg - PCC register bundle
66  *
67  * @vaddr: cached virtual address for this register
68  * @gas: pointer to the generic address structure for this register
69  * @preserve_mask: bitmask to preserve when writing to this register
70  * @set_mask: bitmask to set when writing to this register
71  * @status_mask: bitmask to determine and/or update the status for this register
72  */
73 struct pcc_chan_reg {
74 	void __iomem *vaddr;
75 	struct acpi_generic_address *gas;
76 	u64 preserve_mask;
77 	u64 set_mask;
78 	u64 status_mask;
79 };
80 
81 /**
82  * struct pcc_chan_info - PCC channel specific information
83  *
84  * @chan: PCC channel information with Shared Memory Region info
85  * @db: PCC register bundle for the doorbell register
86  * @plat_irq_ack: PCC register bundle for the platform interrupt acknowledge
87  *	register
88  * @cmd_complete: PCC register bundle for the command complete check register
89  * @cmd_update: PCC register bundle for the command complete update register
90  * @error: PCC register bundle for the error status register
91  * @plat_irq: platform interrupt
92  * @type: PCC subspace type
93  * @plat_irq_flags: platform interrupt flags
94  * @chan_in_use: this flag is used just to check if the interrupt needs
95  *		handling when it is shared. Since only one transfer can occur
96  *		at a time and mailbox takes care of locking, this flag can be
97  *		accessed without a lock. Note: the type only support the
98  *		communication from OSPM to Platform, like type3, use it, and
99  *		other types completely ignore it.
100  */
101 struct pcc_chan_info {
102 	struct pcc_mbox_chan chan;
103 	struct pcc_chan_reg db;
104 	struct pcc_chan_reg plat_irq_ack;
105 	struct pcc_chan_reg cmd_complete;
106 	struct pcc_chan_reg cmd_update;
107 	struct pcc_chan_reg error;
108 	int plat_irq;
109 	u8 type;
110 	unsigned int plat_irq_flags;
111 	bool chan_in_use;
112 };
113 
114 #define to_pcc_chan_info(c) container_of(c, struct pcc_chan_info, chan)
115 static struct pcc_chan_info *chan_info;
116 static int pcc_chan_count;
117 
118 /*
119  * PCC can be used with perf critical drivers such as CPPC
120  * So it makes sense to locally cache the virtual address and
121  * use it to read/write to PCC registers such as doorbell register
122  *
123  * The below read_register and write_registers are used to read and
124  * write from perf critical registers such as PCC doorbell register
125  */
read_register(void __iomem * vaddr,u64 * val,unsigned int bit_width)126 static void read_register(void __iomem *vaddr, u64 *val, unsigned int bit_width)
127 {
128 	switch (bit_width) {
129 	case 8:
130 		*val = readb(vaddr);
131 		break;
132 	case 16:
133 		*val = readw(vaddr);
134 		break;
135 	case 32:
136 		*val = readl(vaddr);
137 		break;
138 	case 64:
139 		*val = readq(vaddr);
140 		break;
141 	}
142 }
143 
write_register(void __iomem * vaddr,u64 val,unsigned int bit_width)144 static void write_register(void __iomem *vaddr, u64 val, unsigned int bit_width)
145 {
146 	switch (bit_width) {
147 	case 8:
148 		writeb(val, vaddr);
149 		break;
150 	case 16:
151 		writew(val, vaddr);
152 		break;
153 	case 32:
154 		writel(val, vaddr);
155 		break;
156 	case 64:
157 		writeq(val, vaddr);
158 		break;
159 	}
160 }
161 
pcc_chan_reg_read(struct pcc_chan_reg * reg,u64 * val)162 static int pcc_chan_reg_read(struct pcc_chan_reg *reg, u64 *val)
163 {
164 	int ret = 0;
165 
166 	if (!reg->gas) {
167 		*val = 0;
168 		return 0;
169 	}
170 
171 	if (reg->vaddr)
172 		read_register(reg->vaddr, val, reg->gas->bit_width);
173 	else
174 		ret = acpi_read(val, reg->gas);
175 
176 	return ret;
177 }
178 
pcc_chan_reg_write(struct pcc_chan_reg * reg,u64 val)179 static int pcc_chan_reg_write(struct pcc_chan_reg *reg, u64 val)
180 {
181 	int ret = 0;
182 
183 	if (!reg->gas)
184 		return 0;
185 
186 	if (reg->vaddr)
187 		write_register(reg->vaddr, val, reg->gas->bit_width);
188 	else
189 		ret = acpi_write(val, reg->gas);
190 
191 	return ret;
192 }
193 
pcc_chan_reg_read_modify_write(struct pcc_chan_reg * reg)194 static int pcc_chan_reg_read_modify_write(struct pcc_chan_reg *reg)
195 {
196 	int ret = 0;
197 	u64 val;
198 
199 	ret = pcc_chan_reg_read(reg, &val);
200 	if (ret)
201 		return ret;
202 
203 	val &= reg->preserve_mask;
204 	val |= reg->set_mask;
205 
206 	return pcc_chan_reg_write(reg, val);
207 }
208 
209 /**
210  * pcc_map_interrupt - Map a PCC subspace GSI to a linux IRQ number
211  * @interrupt: GSI number.
212  * @flags: interrupt flags
213  *
214  * Returns: a valid linux IRQ number on success
215  *		0 or -EINVAL on failure
216  */
pcc_map_interrupt(u32 interrupt,u32 flags)217 static int pcc_map_interrupt(u32 interrupt, u32 flags)
218 {
219 	int trigger, polarity;
220 
221 	if (!interrupt)
222 		return 0;
223 
224 	trigger = (flags & ACPI_PCCT_INTERRUPT_MODE) ? ACPI_EDGE_SENSITIVE
225 			: ACPI_LEVEL_SENSITIVE;
226 
227 	polarity = (flags & ACPI_PCCT_INTERRUPT_POLARITY) ? ACPI_ACTIVE_LOW
228 			: ACPI_ACTIVE_HIGH;
229 
230 	return acpi_register_gsi(NULL, interrupt, trigger, polarity);
231 }
232 
pcc_chan_plat_irq_can_be_shared(struct pcc_chan_info * pchan)233 static bool pcc_chan_plat_irq_can_be_shared(struct pcc_chan_info *pchan)
234 {
235 	return (pchan->plat_irq_flags & ACPI_PCCT_INTERRUPT_MODE) ==
236 		ACPI_LEVEL_SENSITIVE;
237 }
238 
pcc_mbox_cmd_complete_check(struct pcc_chan_info * pchan)239 static bool pcc_mbox_cmd_complete_check(struct pcc_chan_info *pchan)
240 {
241 	u64 val;
242 	int ret;
243 
244 	if (!pchan->cmd_complete.gas)
245 		return true;
246 
247 	ret = pcc_chan_reg_read(&pchan->cmd_complete, &val);
248 	if (ret)
249 		return false;
250 
251 	/*
252 	 * Judge if the channel respond the interrupt based on the value of
253 	 * command complete.
254 	 */
255 	val &= pchan->cmd_complete.status_mask;
256 
257 	/*
258 	 * If this is PCC slave subspace channel, and the command complete
259 	 * bit 0 indicates that Platform is sending a notification and OSPM
260 	 * needs to respond this interrupt to process this command.
261 	 */
262 	if (pchan->type == ACPI_PCCT_TYPE_EXT_PCC_SLAVE_SUBSPACE)
263 		return !val;
264 
265 	return !!val;
266 }
267 
pcc_mbox_error_check_and_clear(struct pcc_chan_info * pchan)268 static int pcc_mbox_error_check_and_clear(struct pcc_chan_info *pchan)
269 {
270 	u64 val;
271 	int ret;
272 
273 	ret = pcc_chan_reg_read(&pchan->error, &val);
274 	if (ret)
275 		return ret;
276 
277 	if (val & pchan->error.status_mask) {
278 		val &= pchan->error.preserve_mask;
279 		pcc_chan_reg_write(&pchan->error, val);
280 		return -EIO;
281 	}
282 
283 	return 0;
284 }
285 
pcc_chan_acknowledge(struct pcc_chan_info * pchan)286 static void pcc_chan_acknowledge(struct pcc_chan_info *pchan)
287 {
288 	struct acpi_pcct_ext_pcc_shared_memory __iomem *pcc_hdr;
289 
290 	if (pchan->type != ACPI_PCCT_TYPE_EXT_PCC_SLAVE_SUBSPACE)
291 		return;
292 
293 	pcc_chan_reg_read_modify_write(&pchan->cmd_update);
294 
295 	pcc_hdr = pchan->chan.shmem;
296 
297 	/*
298 	 * The PCC slave subspace channel needs to set the command
299 	 * complete bit after processing message. If the PCC_ACK_FLAG
300 	 * is set, it should also ring the doorbell.
301 	 */
302 	if (ioread32(&pcc_hdr->flags) & PCC_CMD_COMPLETION_NOTIFY)
303 		pcc_chan_reg_read_modify_write(&pchan->db);
304 }
305 
306 /**
307  * pcc_mbox_irq - PCC mailbox interrupt handler
308  * @irq:	interrupt number
309  * @p: data/cookie passed from the caller to identify the channel
310  *
311  * Returns: IRQ_HANDLED if interrupt is handled or IRQ_NONE if not
312  */
pcc_mbox_irq(int irq,void * p)313 static irqreturn_t pcc_mbox_irq(int irq, void *p)
314 {
315 	struct pcc_chan_info *pchan;
316 	struct mbox_chan *chan = p;
317 
318 	pchan = chan->con_priv;
319 
320 	if (pcc_chan_reg_read_modify_write(&pchan->plat_irq_ack))
321 		return IRQ_NONE;
322 
323 	if (pchan->type == ACPI_PCCT_TYPE_EXT_PCC_MASTER_SUBSPACE &&
324 	    !pchan->chan_in_use)
325 		return IRQ_NONE;
326 
327 	if (!pcc_mbox_cmd_complete_check(pchan))
328 		return IRQ_NONE;
329 
330 	if (pcc_mbox_error_check_and_clear(pchan))
331 		return IRQ_NONE;
332 
333 	/*
334 	 * Clear this flag after updating interrupt ack register and just
335 	 * before mbox_chan_received_data() which might call pcc_send_data()
336 	 * where the flag is set again to start new transfer. This is
337 	 * required to avoid any possible race in updatation of this flag.
338 	 */
339 	pchan->chan_in_use = false;
340 	mbox_chan_received_data(chan, NULL);
341 	mbox_chan_txdone(chan, 0);
342 
343 	pcc_chan_acknowledge(pchan);
344 
345 	return IRQ_HANDLED;
346 }
347 
348 /**
349  * pcc_mbox_request_channel - PCC clients call this function to
350  *		request a pointer to their PCC subspace, from which they
351  *		can get the details of communicating with the remote.
352  * @cl: Pointer to Mailbox client, so we know where to bind the
353  *		Channel.
354  * @subspace_id: The PCC Subspace index as parsed in the PCC client
355  *		ACPI package. This is used to lookup the array of PCC
356  *		subspaces as parsed by the PCC Mailbox controller.
357  *
358  * Return: Pointer to the PCC Mailbox Channel if successful or ERR_PTR.
359  */
360 struct pcc_mbox_chan *
pcc_mbox_request_channel(struct mbox_client * cl,int subspace_id)361 pcc_mbox_request_channel(struct mbox_client *cl, int subspace_id)
362 {
363 	struct pcc_mbox_chan *pcc_mchan;
364 	struct pcc_chan_info *pchan;
365 	struct mbox_chan *chan;
366 	int rc;
367 
368 	if (subspace_id < 0 || subspace_id >= pcc_chan_count)
369 		return ERR_PTR(-ENOENT);
370 
371 	pchan = chan_info + subspace_id;
372 	chan = pchan->chan.mchan;
373 	if (IS_ERR(chan) || chan->cl) {
374 		pr_err("Channel not found for idx: %d\n", subspace_id);
375 		return ERR_PTR(-EBUSY);
376 	}
377 
378 	pcc_mchan = &pchan->chan;
379 	pcc_mchan->shmem = acpi_os_ioremap(pcc_mchan->shmem_base_addr,
380 					   pcc_mchan->shmem_size);
381 	if (!pcc_mchan->shmem)
382 		return ERR_PTR(-ENXIO);
383 
384 	rc = mbox_bind_client(chan, cl);
385 	if (rc) {
386 		iounmap(pcc_mchan->shmem);
387 		pcc_mchan->shmem = NULL;
388 		return ERR_PTR(rc);
389 	}
390 
391 	return pcc_mchan;
392 }
393 EXPORT_SYMBOL_GPL(pcc_mbox_request_channel);
394 
395 /**
396  * pcc_mbox_free_channel - Clients call this to free their Channel.
397  *
398  * @pchan: Pointer to the PCC mailbox channel as returned by
399  *	   pcc_mbox_request_channel()
400  */
pcc_mbox_free_channel(struct pcc_mbox_chan * pchan)401 void pcc_mbox_free_channel(struct pcc_mbox_chan *pchan)
402 {
403 	struct mbox_chan *chan = pchan->mchan;
404 	struct pcc_chan_info *pchan_info;
405 	struct pcc_mbox_chan *pcc_mbox_chan;
406 
407 	if (!chan || !chan->cl)
408 		return;
409 	pchan_info = chan->con_priv;
410 	pcc_mbox_chan = &pchan_info->chan;
411 	if (pcc_mbox_chan->shmem) {
412 		iounmap(pcc_mbox_chan->shmem);
413 		pcc_mbox_chan->shmem = NULL;
414 	}
415 
416 	mbox_free_channel(chan);
417 }
418 EXPORT_SYMBOL_GPL(pcc_mbox_free_channel);
419 
420 /**
421  * pcc_send_data - Called from Mailbox Controller code. Used
422  *		here only to ring the channel doorbell. The PCC client
423  *		specific read/write is done in the client driver in
424  *		order to maintain atomicity over PCC channel once
425  *		OS has control over it. See above for flow of operations.
426  * @chan: Pointer to Mailbox channel over which to send data.
427  * @data: Client specific data written over channel. Used here
428  *		only for debug after PCC transaction completes.
429  *
430  * Return: Err if something failed else 0 for success.
431  */
pcc_send_data(struct mbox_chan * chan,void * data)432 static int pcc_send_data(struct mbox_chan *chan, void *data)
433 {
434 	int ret;
435 	struct pcc_chan_info *pchan = chan->con_priv;
436 
437 	ret = pcc_chan_reg_read_modify_write(&pchan->cmd_update);
438 	if (ret)
439 		return ret;
440 
441 	ret = pcc_chan_reg_read_modify_write(&pchan->db);
442 	if (!ret && pchan->plat_irq > 0)
443 		pchan->chan_in_use = true;
444 
445 	return ret;
446 }
447 
pcc_last_tx_done(struct mbox_chan * chan)448 static bool pcc_last_tx_done(struct mbox_chan *chan)
449 {
450 	struct pcc_chan_info *pchan = chan->con_priv;
451 
452 	return pcc_mbox_cmd_complete_check(pchan);
453 }
454 
455 /**
456  * pcc_startup - Called from Mailbox Controller code. Used here
457  *		to request the interrupt.
458  * @chan: Pointer to Mailbox channel to startup.
459  *
460  * Return: Err if something failed else 0 for success.
461  */
pcc_startup(struct mbox_chan * chan)462 static int pcc_startup(struct mbox_chan *chan)
463 {
464 	struct pcc_chan_info *pchan = chan->con_priv;
465 	unsigned long irqflags;
466 	int rc;
467 
468 	/*
469 	 * Clear and acknowledge any pending interrupts on responder channel
470 	 * before enabling the interrupt
471 	 */
472 	pcc_chan_acknowledge(pchan);
473 
474 	if (pchan->plat_irq > 0) {
475 		irqflags = pcc_chan_plat_irq_can_be_shared(pchan) ?
476 						IRQF_SHARED : 0;
477 		rc = devm_request_irq(chan->mbox->dev, pchan->plat_irq, pcc_mbox_irq,
478 				      irqflags, MBOX_IRQ_NAME, chan);
479 		if (unlikely(rc)) {
480 			dev_err(chan->mbox->dev, "failed to register PCC interrupt %d\n",
481 				pchan->plat_irq);
482 			return rc;
483 		}
484 	}
485 
486 	return 0;
487 }
488 
489 /**
490  * pcc_shutdown - Called from Mailbox Controller code. Used here
491  *		to free the interrupt.
492  * @chan: Pointer to Mailbox channel to shutdown.
493  */
pcc_shutdown(struct mbox_chan * chan)494 static void pcc_shutdown(struct mbox_chan *chan)
495 {
496 	struct pcc_chan_info *pchan = chan->con_priv;
497 
498 	if (pchan->plat_irq > 0)
499 		devm_free_irq(chan->mbox->dev, pchan->plat_irq, chan);
500 }
501 
502 static const struct mbox_chan_ops pcc_chan_ops = {
503 	.send_data = pcc_send_data,
504 	.startup = pcc_startup,
505 	.shutdown = pcc_shutdown,
506 	.last_tx_done = pcc_last_tx_done,
507 };
508 
509 /**
510  * parse_pcc_subspace - Count PCC subspaces defined
511  * @header: Pointer to the ACPI subtable header under the PCCT.
512  * @end: End of subtable entry.
513  *
514  * Return: If we find a PCC subspace entry of a valid type, return 0.
515  *	Otherwise, return -EINVAL.
516  *
517  * This gets called for each entry in the PCC table.
518  */
parse_pcc_subspace(union acpi_subtable_headers * header,const unsigned long end)519 static int parse_pcc_subspace(union acpi_subtable_headers *header,
520 		const unsigned long end)
521 {
522 	struct acpi_pcct_subspace *ss = (struct acpi_pcct_subspace *) header;
523 
524 	if (ss->header.type < ACPI_PCCT_TYPE_RESERVED)
525 		return 0;
526 
527 	return -EINVAL;
528 }
529 
530 static int
pcc_chan_reg_init(struct pcc_chan_reg * reg,struct acpi_generic_address * gas,u64 preserve_mask,u64 set_mask,u64 status_mask,char * name)531 pcc_chan_reg_init(struct pcc_chan_reg *reg, struct acpi_generic_address *gas,
532 		  u64 preserve_mask, u64 set_mask, u64 status_mask, char *name)
533 {
534 	if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
535 		if (!(gas->bit_width >= 8 && gas->bit_width <= 64 &&
536 		      is_power_of_2(gas->bit_width))) {
537 			pr_err("Error: Cannot access register of %u bit width",
538 			       gas->bit_width);
539 			return -EFAULT;
540 		}
541 
542 		reg->vaddr = acpi_os_ioremap(gas->address, gas->bit_width / 8);
543 		if (!reg->vaddr) {
544 			pr_err("Failed to ioremap PCC %s register\n", name);
545 			return -ENOMEM;
546 		}
547 	}
548 	reg->gas = gas;
549 	reg->preserve_mask = preserve_mask;
550 	reg->set_mask = set_mask;
551 	reg->status_mask = status_mask;
552 	return 0;
553 }
554 
555 /**
556  * pcc_parse_subspace_irq - Parse the PCC IRQ and PCC ACK register
557  *
558  * @pchan: Pointer to the PCC channel info structure.
559  * @pcct_entry: Pointer to the ACPI subtable header.
560  *
561  * Return: 0 for Success, else errno.
562  *
563  * There should be one entry per PCC channel. This gets called for each
564  * entry in the PCC table. This uses PCCY Type1 structure for all applicable
565  * types(Type 1-4) to fetch irq
566  */
pcc_parse_subspace_irq(struct pcc_chan_info * pchan,struct acpi_subtable_header * pcct_entry)567 static int pcc_parse_subspace_irq(struct pcc_chan_info *pchan,
568 				  struct acpi_subtable_header *pcct_entry)
569 {
570 	int ret = 0;
571 	struct acpi_pcct_hw_reduced *pcct_ss;
572 
573 	if (pcct_entry->type < ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE ||
574 	    pcct_entry->type > ACPI_PCCT_TYPE_EXT_PCC_SLAVE_SUBSPACE)
575 		return 0;
576 
577 	pcct_ss = (struct acpi_pcct_hw_reduced *)pcct_entry;
578 	pchan->plat_irq = pcc_map_interrupt(pcct_ss->platform_interrupt,
579 					    (u32)pcct_ss->flags);
580 	if (pchan->plat_irq <= 0) {
581 		pr_err("PCC GSI %d not registered\n",
582 		       pcct_ss->platform_interrupt);
583 		return -EINVAL;
584 	}
585 	pchan->plat_irq_flags = pcct_ss->flags;
586 
587 	if (pcct_ss->header.type == ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE_TYPE2) {
588 		struct acpi_pcct_hw_reduced_type2 *pcct2_ss = (void *)pcct_ss;
589 
590 		ret = pcc_chan_reg_init(&pchan->plat_irq_ack,
591 					&pcct2_ss->platform_ack_register,
592 					pcct2_ss->ack_preserve_mask,
593 					pcct2_ss->ack_write_mask, 0,
594 					"PLAT IRQ ACK");
595 
596 	} else if (pcct_ss->header.type == ACPI_PCCT_TYPE_EXT_PCC_MASTER_SUBSPACE ||
597 		   pcct_ss->header.type == ACPI_PCCT_TYPE_EXT_PCC_SLAVE_SUBSPACE) {
598 		struct acpi_pcct_ext_pcc_master *pcct_ext = (void *)pcct_ss;
599 
600 		ret = pcc_chan_reg_init(&pchan->plat_irq_ack,
601 					&pcct_ext->platform_ack_register,
602 					pcct_ext->ack_preserve_mask,
603 					pcct_ext->ack_set_mask, 0,
604 					"PLAT IRQ ACK");
605 	}
606 
607 	if (pcc_chan_plat_irq_can_be_shared(pchan) &&
608 	    !pchan->plat_irq_ack.gas) {
609 		pr_err("PCC subspace has level IRQ with no ACK register\n");
610 		return -EINVAL;
611 	}
612 
613 	return ret;
614 }
615 
616 /**
617  * pcc_parse_subspace_db_reg - Parse the PCC doorbell register
618  *
619  * @pchan: Pointer to the PCC channel info structure.
620  * @pcct_entry: Pointer to the ACPI subtable header.
621  *
622  * Return: 0 for Success, else errno.
623  */
pcc_parse_subspace_db_reg(struct pcc_chan_info * pchan,struct acpi_subtable_header * pcct_entry)624 static int pcc_parse_subspace_db_reg(struct pcc_chan_info *pchan,
625 				     struct acpi_subtable_header *pcct_entry)
626 {
627 	int ret = 0;
628 
629 	if (pcct_entry->type <= ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE_TYPE2) {
630 		struct acpi_pcct_subspace *pcct_ss;
631 
632 		pcct_ss = (struct acpi_pcct_subspace *)pcct_entry;
633 
634 		ret = pcc_chan_reg_init(&pchan->db,
635 					&pcct_ss->doorbell_register,
636 					pcct_ss->preserve_mask,
637 					pcct_ss->write_mask, 0,	"Doorbell");
638 
639 	} else {
640 		struct acpi_pcct_ext_pcc_master *pcct_ext;
641 
642 		pcct_ext = (struct acpi_pcct_ext_pcc_master *)pcct_entry;
643 
644 		ret = pcc_chan_reg_init(&pchan->db,
645 					&pcct_ext->doorbell_register,
646 					pcct_ext->preserve_mask,
647 					pcct_ext->write_mask, 0, "Doorbell");
648 		if (ret)
649 			return ret;
650 
651 		ret = pcc_chan_reg_init(&pchan->cmd_complete,
652 					&pcct_ext->cmd_complete_register,
653 					0, 0, pcct_ext->cmd_complete_mask,
654 					"Command Complete Check");
655 		if (ret)
656 			return ret;
657 
658 		ret = pcc_chan_reg_init(&pchan->cmd_update,
659 					&pcct_ext->cmd_update_register,
660 					pcct_ext->cmd_update_preserve_mask,
661 					pcct_ext->cmd_update_set_mask, 0,
662 					"Command Complete Update");
663 		if (ret)
664 			return ret;
665 
666 		ret = pcc_chan_reg_init(&pchan->error,
667 					&pcct_ext->error_status_register,
668 					~pcct_ext->error_status_mask, 0,
669 					pcct_ext->error_status_mask,
670 					"Error Status");
671 	}
672 	return ret;
673 }
674 
675 /**
676  * pcc_parse_subspace_shmem - Parse the PCC Shared Memory Region information
677  *
678  * @pchan: Pointer to the PCC channel info structure.
679  * @pcct_entry: Pointer to the ACPI subtable header.
680  *
681  */
pcc_parse_subspace_shmem(struct pcc_chan_info * pchan,struct acpi_subtable_header * pcct_entry)682 static void pcc_parse_subspace_shmem(struct pcc_chan_info *pchan,
683 				     struct acpi_subtable_header *pcct_entry)
684 {
685 	if (pcct_entry->type <= ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE_TYPE2) {
686 		struct acpi_pcct_subspace *pcct_ss =
687 			(struct acpi_pcct_subspace *)pcct_entry;
688 
689 		pchan->chan.shmem_base_addr = pcct_ss->base_address;
690 		pchan->chan.shmem_size = pcct_ss->length;
691 		pchan->chan.latency = pcct_ss->latency;
692 		pchan->chan.max_access_rate = pcct_ss->max_access_rate;
693 		pchan->chan.min_turnaround_time = pcct_ss->min_turnaround_time;
694 	} else {
695 		struct acpi_pcct_ext_pcc_master *pcct_ext =
696 			(struct acpi_pcct_ext_pcc_master *)pcct_entry;
697 
698 		pchan->chan.shmem_base_addr = pcct_ext->base_address;
699 		pchan->chan.shmem_size = pcct_ext->length;
700 		pchan->chan.latency = pcct_ext->latency;
701 		pchan->chan.max_access_rate = pcct_ext->max_access_rate;
702 		pchan->chan.min_turnaround_time = pcct_ext->min_turnaround_time;
703 	}
704 }
705 
706 /**
707  * acpi_pcc_probe - Parse the ACPI tree for the PCCT.
708  *
709  * Return: 0 for Success, else errno.
710  */
acpi_pcc_probe(void)711 static int __init acpi_pcc_probe(void)
712 {
713 	int count, i, rc = 0;
714 	acpi_status status;
715 	struct acpi_table_header *pcct_tbl;
716 	struct acpi_subtable_proc proc[ACPI_PCCT_TYPE_RESERVED];
717 
718 	status = acpi_get_table(ACPI_SIG_PCCT, 0, &pcct_tbl);
719 	if (ACPI_FAILURE(status) || !pcct_tbl)
720 		return -ENODEV;
721 
722 	/* Set up the subtable handlers */
723 	for (i = ACPI_PCCT_TYPE_GENERIC_SUBSPACE;
724 	     i < ACPI_PCCT_TYPE_RESERVED; i++) {
725 		proc[i].id = i;
726 		proc[i].count = 0;
727 		proc[i].handler = parse_pcc_subspace;
728 	}
729 
730 	count = acpi_table_parse_entries_array(ACPI_SIG_PCCT,
731 			sizeof(struct acpi_table_pcct), proc,
732 			ACPI_PCCT_TYPE_RESERVED, MAX_PCC_SUBSPACES);
733 	if (count <= 0 || count > MAX_PCC_SUBSPACES) {
734 		if (count < 0)
735 			pr_warn("Error parsing PCC subspaces from PCCT\n");
736 		else
737 			pr_warn("Invalid PCCT: %d PCC subspaces\n", count);
738 
739 		rc = -EINVAL;
740 	} else {
741 		pcc_chan_count = count;
742 	}
743 
744 	acpi_put_table(pcct_tbl);
745 
746 	return rc;
747 }
748 
749 /**
750  * pcc_mbox_probe - Called when we find a match for the
751  *	PCCT platform device. This is purely used to represent
752  *	the PCCT as a virtual device for registering with the
753  *	generic Mailbox framework.
754  *
755  * @pdev: Pointer to platform device returned when a match
756  *	is found.
757  *
758  *	Return: 0 for Success, else errno.
759  */
pcc_mbox_probe(struct platform_device * pdev)760 static int pcc_mbox_probe(struct platform_device *pdev)
761 {
762 	struct device *dev = &pdev->dev;
763 	struct mbox_controller *pcc_mbox_ctrl;
764 	struct mbox_chan *pcc_mbox_channels;
765 	struct acpi_table_header *pcct_tbl;
766 	struct acpi_subtable_header *pcct_entry;
767 	struct acpi_table_pcct *acpi_pcct_tbl;
768 	acpi_status status = AE_OK;
769 	int i, rc, count = pcc_chan_count;
770 
771 	/* Search for PCCT */
772 	status = acpi_get_table(ACPI_SIG_PCCT, 0, &pcct_tbl);
773 
774 	if (ACPI_FAILURE(status) || !pcct_tbl)
775 		return -ENODEV;
776 
777 	pcc_mbox_channels = devm_kcalloc(dev, count, sizeof(*pcc_mbox_channels),
778 					 GFP_KERNEL);
779 	if (!pcc_mbox_channels) {
780 		rc = -ENOMEM;
781 		goto err;
782 	}
783 
784 	chan_info = devm_kcalloc(dev, count, sizeof(*chan_info), GFP_KERNEL);
785 	if (!chan_info) {
786 		rc = -ENOMEM;
787 		goto err;
788 	}
789 
790 	pcc_mbox_ctrl = devm_kzalloc(dev, sizeof(*pcc_mbox_ctrl), GFP_KERNEL);
791 	if (!pcc_mbox_ctrl) {
792 		rc = -ENOMEM;
793 		goto err;
794 	}
795 
796 	/* Point to the first PCC subspace entry */
797 	pcct_entry = (struct acpi_subtable_header *) (
798 		(unsigned long) pcct_tbl + sizeof(struct acpi_table_pcct));
799 
800 	acpi_pcct_tbl = (struct acpi_table_pcct *) pcct_tbl;
801 	if (acpi_pcct_tbl->flags & ACPI_PCCT_DOORBELL) {
802 		pcc_mbox_ctrl->txdone_irq = true;
803 		pcc_mbox_ctrl->txdone_poll = false;
804 	} else {
805 		pcc_mbox_ctrl->txdone_irq = false;
806 		pcc_mbox_ctrl->txdone_poll = true;
807 	}
808 
809 	for (i = 0; i < count; i++) {
810 		struct pcc_chan_info *pchan = chan_info + i;
811 
812 		pcc_mbox_channels[i].con_priv = pchan;
813 		pchan->chan.mchan = &pcc_mbox_channels[i];
814 
815 		if (pcct_entry->type == ACPI_PCCT_TYPE_EXT_PCC_SLAVE_SUBSPACE &&
816 		    !pcc_mbox_ctrl->txdone_irq) {
817 			pr_err("Platform Interrupt flag must be set to 1");
818 			rc = -EINVAL;
819 			goto err;
820 		}
821 
822 		if (pcc_mbox_ctrl->txdone_irq) {
823 			rc = pcc_parse_subspace_irq(pchan, pcct_entry);
824 			if (rc < 0)
825 				goto err;
826 		}
827 		rc = pcc_parse_subspace_db_reg(pchan, pcct_entry);
828 		if (rc < 0)
829 			goto err;
830 
831 		pcc_parse_subspace_shmem(pchan, pcct_entry);
832 
833 		pchan->type = pcct_entry->type;
834 		pcct_entry = (struct acpi_subtable_header *)
835 			((unsigned long) pcct_entry + pcct_entry->length);
836 	}
837 
838 	pcc_mbox_ctrl->num_chans = count;
839 
840 	pr_info("Detected %d PCC Subspaces\n", pcc_mbox_ctrl->num_chans);
841 
842 	pcc_mbox_ctrl->chans = pcc_mbox_channels;
843 	pcc_mbox_ctrl->ops = &pcc_chan_ops;
844 	pcc_mbox_ctrl->dev = dev;
845 
846 	pr_info("Registering PCC driver as Mailbox controller\n");
847 	rc = mbox_controller_register(pcc_mbox_ctrl);
848 	if (rc)
849 		pr_err("Err registering PCC as Mailbox controller: %d\n", rc);
850 	else
851 		return 0;
852 err:
853 	acpi_put_table(pcct_tbl);
854 	return rc;
855 }
856 
857 static struct platform_driver pcc_mbox_driver = {
858 	.probe = pcc_mbox_probe,
859 	.driver = {
860 		.name = "PCCT",
861 	},
862 };
863 
pcc_init(void)864 static int __init pcc_init(void)
865 {
866 	int ret;
867 	struct platform_device *pcc_pdev;
868 
869 	if (acpi_disabled)
870 		return -ENODEV;
871 
872 	/* Check if PCC support is available. */
873 	ret = acpi_pcc_probe();
874 
875 	if (ret) {
876 		pr_debug("ACPI PCC probe failed.\n");
877 		return -ENODEV;
878 	}
879 
880 	pcc_pdev = platform_create_bundle(&pcc_mbox_driver,
881 			pcc_mbox_probe, NULL, 0, NULL, 0);
882 
883 	if (IS_ERR(pcc_pdev)) {
884 		pr_debug("Err creating PCC platform bundle\n");
885 		pcc_chan_count = 0;
886 		return PTR_ERR(pcc_pdev);
887 	}
888 
889 	return 0;
890 }
891 
892 /*
893  * Make PCC init postcore so that users of this mailbox
894  * such as the ACPI Processor driver have it available
895  * at their init.
896  */
897 postcore_initcall(pcc_init);
898