Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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