xref: /openbmc/linux/drivers/mailbox/pcc.c (revision b802fb99)
1 /*
2  *	Copyright (C) 2014 Linaro Ltd.
3  *	Author:	Ashwin Chaugule <ashwin.chaugule@linaro.org>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  PCC (Platform Communication Channel) is defined in the ACPI 5.0+
16  *  specification. It is a mailbox like mechanism to allow clients
17  *  such as CPPC (Collaborative Processor Performance Control), RAS
18  *  (Reliability, Availability and Serviceability) and MPST (Memory
19  *  Node Power State Table) to talk to the platform (e.g. BMC) through
20  *  shared memory regions as defined in the PCC table entries. The PCC
21  *  specification supports a Doorbell mechanism for the PCC clients
22  *  to notify the platform about new data. This Doorbell information
23  *  is also specified in each PCC table entry.
24  *
25  *  Typical high level flow of operation is:
26  *
27  *  PCC Reads:
28  *  * Client tries to acquire a channel lock.
29  *  * After it is acquired it writes READ cmd in communication region cmd
30  *		address.
31  *  * Client issues mbox_send_message() which rings the PCC doorbell
32  *		for its PCC channel.
33  *  * If command completes, then client has control over channel and
34  *		it can proceed with its reads.
35  *  * Client releases lock.
36  *
37  *  PCC Writes:
38  *  * Client tries to acquire channel lock.
39  *  * Client writes to its communication region after it acquires a
40  *		channel lock.
41  *  * Client writes WRITE cmd in communication region cmd address.
42  *  * Client issues mbox_send_message() which rings the PCC doorbell
43  *		for its PCC channel.
44  *  * If command completes, then writes have succeded and it can release
45  *		the channel lock.
46  *
47  *  There is a Nominal latency defined for each channel which indicates
48  *  how long to wait until a command completes. If command is not complete
49  *  the client needs to retry or assume failure.
50  *
51  *	For more details about PCC, please see the ACPI specification from
52  *  http://www.uefi.org/ACPIv5.1 Section 14.
53  *
54  *  This file implements PCC as a Mailbox controller and allows for PCC
55  *  clients to be implemented as its Mailbox Client Channels.
56  */
57 
58 #include <linux/acpi.h>
59 #include <linux/delay.h>
60 #include <linux/io.h>
61 #include <linux/init.h>
62 #include <linux/list.h>
63 #include <linux/platform_device.h>
64 #include <linux/mailbox_controller.h>
65 #include <linux/mailbox_client.h>
66 
67 #include "mailbox.h"
68 
69 #define MAX_PCC_SUBSPACES	256
70 
71 static struct mbox_chan *pcc_mbox_channels;
72 
73 static struct mbox_controller pcc_mbox_ctrl = {};
74 /**
75  * get_pcc_channel - Given a PCC subspace idx, get
76  *	the respective mbox_channel.
77  * @id: PCC subspace index.
78  *
79  * Return: ERR_PTR(errno) if error, else pointer
80  *	to mbox channel.
81  */
82 static struct mbox_chan *get_pcc_channel(int id)
83 {
84 	if (id < 0 || id > pcc_mbox_ctrl.num_chans)
85 		return ERR_PTR(-ENOENT);
86 
87 	return &pcc_mbox_channels[id];
88 }
89 
90 /**
91  * pcc_mbox_request_channel - PCC clients call this function to
92  *		request a pointer to their PCC subspace, from which they
93  *		can get the details of communicating with the remote.
94  * @cl: Pointer to Mailbox client, so we know where to bind the
95  *		Channel.
96  * @subspace_id: The PCC Subspace index as parsed in the PCC client
97  *		ACPI package. This is used to lookup the array of PCC
98  *		subspaces as parsed by the PCC Mailbox controller.
99  *
100  * Return: Pointer to the Mailbox Channel if successful or
101  *		ERR_PTR.
102  */
103 struct mbox_chan *pcc_mbox_request_channel(struct mbox_client *cl,
104 		int subspace_id)
105 {
106 	struct device *dev = pcc_mbox_ctrl.dev;
107 	struct mbox_chan *chan;
108 	unsigned long flags;
109 
110 	/*
111 	 * Each PCC Subspace is a Mailbox Channel.
112 	 * The PCC Clients get their PCC Subspace ID
113 	 * from their own tables and pass it here.
114 	 * This returns a pointer to the PCC subspace
115 	 * for the Client to operate on.
116 	 */
117 	chan = get_pcc_channel(subspace_id);
118 
119 	if (IS_ERR(chan) || chan->cl) {
120 		dev_err(dev, "Channel not found for idx: %d\n", subspace_id);
121 		return ERR_PTR(-EBUSY);
122 	}
123 
124 	spin_lock_irqsave(&chan->lock, flags);
125 	chan->msg_free = 0;
126 	chan->msg_count = 0;
127 	chan->active_req = NULL;
128 	chan->cl = cl;
129 	init_completion(&chan->tx_complete);
130 
131 	if (chan->txdone_method == TXDONE_BY_POLL && cl->knows_txdone)
132 		chan->txdone_method |= TXDONE_BY_ACK;
133 
134 	spin_unlock_irqrestore(&chan->lock, flags);
135 
136 	return chan;
137 }
138 EXPORT_SYMBOL_GPL(pcc_mbox_request_channel);
139 
140 /**
141  * pcc_mbox_free_channel - Clients call this to free their Channel.
142  *
143  * @chan: Pointer to the mailbox channel as returned by
144  *		pcc_mbox_request_channel()
145  */
146 void pcc_mbox_free_channel(struct mbox_chan *chan)
147 {
148 	unsigned long flags;
149 
150 	if (!chan || !chan->cl)
151 		return;
152 
153 	spin_lock_irqsave(&chan->lock, flags);
154 	chan->cl = NULL;
155 	chan->active_req = NULL;
156 	if (chan->txdone_method == (TXDONE_BY_POLL | TXDONE_BY_ACK))
157 		chan->txdone_method = TXDONE_BY_POLL;
158 
159 	spin_unlock_irqrestore(&chan->lock, flags);
160 }
161 EXPORT_SYMBOL_GPL(pcc_mbox_free_channel);
162 
163 /**
164  * pcc_send_data - Called from Mailbox Controller code. Used
165  *		here only to ring the channel doorbell. The PCC client
166  *		specific read/write is done in the client driver in
167  *		order to maintain atomicity over PCC channel once
168  *		OS has control over it. See above for flow of operations.
169  * @chan: Pointer to Mailbox channel over which to send data.
170  * @data: Client specific data written over channel. Used here
171  *		only for debug after PCC transaction completes.
172  *
173  * Return: Err if something failed else 0 for success.
174  */
175 static int pcc_send_data(struct mbox_chan *chan, void *data)
176 {
177 	struct acpi_pcct_hw_reduced *pcct_ss = chan->con_priv;
178 	struct acpi_generic_address doorbell;
179 	u64 doorbell_preserve;
180 	u64 doorbell_val;
181 	u64 doorbell_write;
182 
183 	doorbell = pcct_ss->doorbell_register;
184 	doorbell_preserve = pcct_ss->preserve_mask;
185 	doorbell_write = pcct_ss->write_mask;
186 
187 	/* Sync notification from OS to Platform. */
188 	acpi_read(&doorbell_val, &doorbell);
189 	acpi_write((doorbell_val & doorbell_preserve) | doorbell_write,
190 			&doorbell);
191 
192 	return 0;
193 }
194 
195 static const struct mbox_chan_ops pcc_chan_ops = {
196 	.send_data = pcc_send_data,
197 };
198 
199 /**
200  * parse_pcc_subspace - Parse the PCC table and verify PCC subspace
201  *		entries. There should be one entry per PCC client.
202  * @header: Pointer to the ACPI subtable header under the PCCT.
203  * @end: End of subtable entry.
204  *
205  * Return: 0 for Success, else errno.
206  *
207  * This gets called for each entry in the PCC table.
208  */
209 static int parse_pcc_subspace(struct acpi_subtable_header *header,
210 		const unsigned long end)
211 {
212 	struct acpi_pcct_hw_reduced *pcct_ss;
213 
214 	if (pcc_mbox_ctrl.num_chans <= MAX_PCC_SUBSPACES) {
215 		pcct_ss = (struct acpi_pcct_hw_reduced *) header;
216 
217 		if (pcct_ss->header.type !=
218 				ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE) {
219 			pr_err("Incorrect PCC Subspace type detected\n");
220 			return -EINVAL;
221 		}
222 	}
223 
224 	return 0;
225 }
226 
227 /**
228  * acpi_pcc_probe - Parse the ACPI tree for the PCCT.
229  *
230  * Return: 0 for Success, else errno.
231  */
232 static int __init acpi_pcc_probe(void)
233 {
234 	acpi_size pcct_tbl_header_size;
235 	struct acpi_table_header *pcct_tbl;
236 	struct acpi_subtable_header *pcct_entry;
237 	int count, i;
238 	acpi_status status = AE_OK;
239 
240 	/* Search for PCCT */
241 	status = acpi_get_table_with_size(ACPI_SIG_PCCT, 0,
242 			&pcct_tbl,
243 			&pcct_tbl_header_size);
244 
245 	if (ACPI_FAILURE(status) || !pcct_tbl) {
246 		pr_warn("PCCT header not found.\n");
247 		return -ENODEV;
248 	}
249 
250 	count = acpi_table_parse_entries(ACPI_SIG_PCCT,
251 			sizeof(struct acpi_table_pcct),
252 			ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE,
253 			parse_pcc_subspace, MAX_PCC_SUBSPACES);
254 
255 	if (count <= 0) {
256 		pr_err("Error parsing PCC subspaces from PCCT\n");
257 		return -EINVAL;
258 	}
259 
260 	pcc_mbox_channels = kzalloc(sizeof(struct mbox_chan) *
261 			count, GFP_KERNEL);
262 
263 	if (!pcc_mbox_channels) {
264 		pr_err("Could not allocate space for PCC mbox channels\n");
265 		return -ENOMEM;
266 	}
267 
268 	/* Point to the first PCC subspace entry */
269 	pcct_entry = (struct acpi_subtable_header *) (
270 		(unsigned long) pcct_tbl + sizeof(struct acpi_table_pcct));
271 
272 	for (i = 0; i < count; i++) {
273 		pcc_mbox_channels[i].con_priv = pcct_entry;
274 		pcct_entry = (struct acpi_subtable_header *)
275 			((unsigned long) pcct_entry + pcct_entry->length);
276 	}
277 
278 	pcc_mbox_ctrl.num_chans = count;
279 
280 	pr_info("Detected %d PCC Subspaces\n", pcc_mbox_ctrl.num_chans);
281 
282 	return 0;
283 }
284 
285 /**
286  * pcc_mbox_probe - Called when we find a match for the
287  *	PCCT platform device. This is purely used to represent
288  *	the PCCT as a virtual device for registering with the
289  *	generic Mailbox framework.
290  *
291  * @pdev: Pointer to platform device returned when a match
292  *	is found.
293  *
294  *	Return: 0 for Success, else errno.
295  */
296 static int pcc_mbox_probe(struct platform_device *pdev)
297 {
298 	int ret = 0;
299 
300 	pcc_mbox_ctrl.chans = pcc_mbox_channels;
301 	pcc_mbox_ctrl.ops = &pcc_chan_ops;
302 	pcc_mbox_ctrl.dev = &pdev->dev;
303 
304 	pr_info("Registering PCC driver as Mailbox controller\n");
305 	ret = mbox_controller_register(&pcc_mbox_ctrl);
306 
307 	if (ret) {
308 		pr_err("Err registering PCC as Mailbox controller: %d\n", ret);
309 		ret = -ENODEV;
310 	}
311 
312 	return ret;
313 }
314 
315 struct platform_driver pcc_mbox_driver = {
316 	.probe = pcc_mbox_probe,
317 	.driver = {
318 		.name = "PCCT",
319 		.owner = THIS_MODULE,
320 	},
321 };
322 
323 static int __init pcc_init(void)
324 {
325 	int ret;
326 	struct platform_device *pcc_pdev;
327 
328 	if (acpi_disabled)
329 		return -ENODEV;
330 
331 	/* Check if PCC support is available. */
332 	ret = acpi_pcc_probe();
333 
334 	if (ret) {
335 		pr_debug("ACPI PCC probe failed.\n");
336 		return -ENODEV;
337 	}
338 
339 	pcc_pdev = platform_create_bundle(&pcc_mbox_driver,
340 			pcc_mbox_probe, NULL, 0, NULL, 0);
341 
342 	if (IS_ERR(pcc_pdev)) {
343 		pr_debug("Err creating PCC platform bundle\n");
344 		return PTR_ERR(pcc_pdev);
345 	}
346 
347 	return 0;
348 }
349 
350 /*
351  * Make PCC init postcore so that users of this mailbox
352  * such as the ACPI Processor driver have it available
353  * at their init.
354  */
355 postcore_initcall(pcc_init);
356