]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/mailbox/pcc.c
mailbox: pcc: Don't access an unmapped memory address space
[mirror_ubuntu-bionic-kernel.git] / drivers / mailbox / pcc.c
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 #include <linux/io-64-nonatomic-lo-hi.h>
67
68 #include "mailbox.h"
69
70 #define MAX_PCC_SUBSPACES 256
71
72 static struct mbox_chan *pcc_mbox_channels;
73
74 /* Array of cached virtual address for doorbell registers */
75 static void __iomem **pcc_doorbell_vaddr;
76
77 static struct mbox_controller pcc_mbox_ctrl = {};
78 /**
79 * get_pcc_channel - Given a PCC subspace idx, get
80 * the respective mbox_channel.
81 * @id: PCC subspace index.
82 *
83 * Return: ERR_PTR(errno) if error, else pointer
84 * to mbox channel.
85 */
86 static struct mbox_chan *get_pcc_channel(int id)
87 {
88 struct mbox_chan *pcc_chan;
89
90 if (id < 0 || id > pcc_mbox_ctrl.num_chans)
91 return ERR_PTR(-ENOENT);
92
93 pcc_chan = (struct mbox_chan *)
94 (unsigned long) pcc_mbox_channels +
95 (id * sizeof(*pcc_chan));
96
97 return pcc_chan;
98 }
99
100 /**
101 * pcc_mbox_request_channel - PCC clients call this function to
102 * request a pointer to their PCC subspace, from which they
103 * can get the details of communicating with the remote.
104 * @cl: Pointer to Mailbox client, so we know where to bind the
105 * Channel.
106 * @subspace_id: The PCC Subspace index as parsed in the PCC client
107 * ACPI package. This is used to lookup the array of PCC
108 * subspaces as parsed by the PCC Mailbox controller.
109 *
110 * Return: Pointer to the Mailbox Channel if successful or
111 * ERR_PTR.
112 */
113 struct mbox_chan *pcc_mbox_request_channel(struct mbox_client *cl,
114 int subspace_id)
115 {
116 struct device *dev = pcc_mbox_ctrl.dev;
117 struct mbox_chan *chan;
118 unsigned long flags;
119
120 /*
121 * Each PCC Subspace is a Mailbox Channel.
122 * The PCC Clients get their PCC Subspace ID
123 * from their own tables and pass it here.
124 * This returns a pointer to the PCC subspace
125 * for the Client to operate on.
126 */
127 chan = get_pcc_channel(subspace_id);
128
129 if (IS_ERR(chan) || chan->cl) {
130 dev_err(dev, "Channel not found for idx: %d\n", subspace_id);
131 return ERR_PTR(-EBUSY);
132 }
133
134 spin_lock_irqsave(&chan->lock, flags);
135 chan->msg_free = 0;
136 chan->msg_count = 0;
137 chan->active_req = NULL;
138 chan->cl = cl;
139 init_completion(&chan->tx_complete);
140
141 if (chan->txdone_method == TXDONE_BY_POLL && cl->knows_txdone)
142 chan->txdone_method |= TXDONE_BY_ACK;
143
144 spin_unlock_irqrestore(&chan->lock, flags);
145
146 return chan;
147 }
148 EXPORT_SYMBOL_GPL(pcc_mbox_request_channel);
149
150 /**
151 * pcc_mbox_free_channel - Clients call this to free their Channel.
152 *
153 * @chan: Pointer to the mailbox channel as returned by
154 * pcc_mbox_request_channel()
155 */
156 void pcc_mbox_free_channel(struct mbox_chan *chan)
157 {
158 unsigned long flags;
159
160 if (!chan || !chan->cl)
161 return;
162
163 spin_lock_irqsave(&chan->lock, flags);
164 chan->cl = NULL;
165 chan->active_req = NULL;
166 if (chan->txdone_method == (TXDONE_BY_POLL | TXDONE_BY_ACK))
167 chan->txdone_method = TXDONE_BY_POLL;
168
169 spin_unlock_irqrestore(&chan->lock, flags);
170 }
171 EXPORT_SYMBOL_GPL(pcc_mbox_free_channel);
172
173 /*
174 * PCC can be used with perf critical drivers such as CPPC
175 * So it makes sense to locally cache the virtual address and
176 * use it to read/write to PCC registers such as doorbell register
177 *
178 * The below read_register and write_registers are used to read and
179 * write from perf critical registers such as PCC doorbell register
180 */
181 static int read_register(void __iomem *vaddr, u64 *val, unsigned int bit_width)
182 {
183 int ret_val = 0;
184
185 switch (bit_width) {
186 case 8:
187 *val = readb(vaddr);
188 break;
189 case 16:
190 *val = readw(vaddr);
191 break;
192 case 32:
193 *val = readl(vaddr);
194 break;
195 case 64:
196 *val = readq(vaddr);
197 break;
198 default:
199 pr_debug("Error: Cannot read register of %u bit width",
200 bit_width);
201 ret_val = -EFAULT;
202 break;
203 }
204 return ret_val;
205 }
206
207 static int write_register(void __iomem *vaddr, u64 val, unsigned int bit_width)
208 {
209 int ret_val = 0;
210
211 switch (bit_width) {
212 case 8:
213 writeb(val, vaddr);
214 break;
215 case 16:
216 writew(val, vaddr);
217 break;
218 case 32:
219 writel(val, vaddr);
220 break;
221 case 64:
222 writeq(val, vaddr);
223 break;
224 default:
225 pr_debug("Error: Cannot write register of %u bit width",
226 bit_width);
227 ret_val = -EFAULT;
228 break;
229 }
230 return ret_val;
231 }
232
233 /**
234 * pcc_send_data - Called from Mailbox Controller code. Used
235 * here only to ring the channel doorbell. The PCC client
236 * specific read/write is done in the client driver in
237 * order to maintain atomicity over PCC channel once
238 * OS has control over it. See above for flow of operations.
239 * @chan: Pointer to Mailbox channel over which to send data.
240 * @data: Client specific data written over channel. Used here
241 * only for debug after PCC transaction completes.
242 *
243 * Return: Err if something failed else 0 for success.
244 */
245 static int pcc_send_data(struct mbox_chan *chan, void *data)
246 {
247 struct acpi_pcct_hw_reduced *pcct_ss = chan->con_priv;
248 struct acpi_generic_address *doorbell;
249 u64 doorbell_preserve;
250 u64 doorbell_val;
251 u64 doorbell_write;
252 u32 id = chan - pcc_mbox_channels;
253 int ret = 0;
254
255 if (id >= pcc_mbox_ctrl.num_chans) {
256 pr_debug("pcc_send_data: Invalid mbox_chan passed\n");
257 return -ENOENT;
258 }
259
260 doorbell = &pcct_ss->doorbell_register;
261 doorbell_preserve = pcct_ss->preserve_mask;
262 doorbell_write = pcct_ss->write_mask;
263
264 /* Sync notification from OS to Platform. */
265 if (pcc_doorbell_vaddr[id]) {
266 ret = read_register(pcc_doorbell_vaddr[id], &doorbell_val,
267 doorbell->bit_width);
268 if (ret)
269 return ret;
270 ret = write_register(pcc_doorbell_vaddr[id],
271 (doorbell_val & doorbell_preserve) | doorbell_write,
272 doorbell->bit_width);
273 } else {
274 ret = acpi_read(&doorbell_val, doorbell);
275 if (ret)
276 return ret;
277 ret = acpi_write((doorbell_val & doorbell_preserve) | doorbell_write,
278 doorbell);
279 }
280 return ret;
281 }
282
283 static const struct mbox_chan_ops pcc_chan_ops = {
284 .send_data = pcc_send_data,
285 };
286
287 /**
288 * parse_pcc_subspace - Parse the PCC table and verify PCC subspace
289 * entries. There should be one entry per PCC client.
290 * @header: Pointer to the ACPI subtable header under the PCCT.
291 * @end: End of subtable entry.
292 *
293 * Return: 0 for Success, else errno.
294 *
295 * This gets called for each entry in the PCC table.
296 */
297 static int parse_pcc_subspace(struct acpi_subtable_header *header,
298 const unsigned long end)
299 {
300 struct acpi_pcct_hw_reduced *pcct_ss;
301
302 if (pcc_mbox_ctrl.num_chans <= MAX_PCC_SUBSPACES) {
303 pcct_ss = (struct acpi_pcct_hw_reduced *) header;
304
305 if (pcct_ss->header.type !=
306 ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE) {
307 pr_err("Incorrect PCC Subspace type detected\n");
308 return -EINVAL;
309 }
310 }
311
312 return 0;
313 }
314
315 /**
316 * acpi_pcc_probe - Parse the ACPI tree for the PCCT.
317 *
318 * Return: 0 for Success, else errno.
319 */
320 static int __init acpi_pcc_probe(void)
321 {
322 acpi_size pcct_tbl_header_size;
323 struct acpi_table_header *pcct_tbl;
324 struct acpi_subtable_header *pcct_entry;
325 int count, i;
326 acpi_status status = AE_OK;
327
328 /* Search for PCCT */
329 status = acpi_get_table_with_size(ACPI_SIG_PCCT, 0,
330 &pcct_tbl,
331 &pcct_tbl_header_size);
332
333 if (ACPI_FAILURE(status) || !pcct_tbl) {
334 pr_warn("PCCT header not found.\n");
335 return -ENODEV;
336 }
337
338 count = acpi_table_parse_entries(ACPI_SIG_PCCT,
339 sizeof(struct acpi_table_pcct),
340 ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE,
341 parse_pcc_subspace, MAX_PCC_SUBSPACES);
342
343 if (count <= 0) {
344 pr_err("Error parsing PCC subspaces from PCCT\n");
345 return -EINVAL;
346 }
347
348 pcc_mbox_channels = kzalloc(sizeof(struct mbox_chan) *
349 count, GFP_KERNEL);
350
351 if (!pcc_mbox_channels) {
352 pr_err("Could not allocate space for PCC mbox channels\n");
353 return -ENOMEM;
354 }
355
356 pcc_doorbell_vaddr = kcalloc(count, sizeof(void *), GFP_KERNEL);
357 if (!pcc_doorbell_vaddr) {
358 kfree(pcc_mbox_channels);
359 return -ENOMEM;
360 }
361
362 /* Point to the first PCC subspace entry */
363 pcct_entry = (struct acpi_subtable_header *) (
364 (unsigned long) pcct_tbl + sizeof(struct acpi_table_pcct));
365
366 for (i = 0; i < count; i++) {
367 struct acpi_generic_address *db_reg;
368 struct acpi_pcct_hw_reduced *pcct_ss;
369 pcc_mbox_channels[i].con_priv = pcct_entry;
370
371 /* If doorbell is in system memory cache the virt address */
372 pcct_ss = (struct acpi_pcct_hw_reduced *)pcct_entry;
373 db_reg = &pcct_ss->doorbell_register;
374 if (db_reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
375 pcc_doorbell_vaddr[i] = acpi_os_ioremap(db_reg->address,
376 db_reg->bit_width/8);
377 pcct_entry = (struct acpi_subtable_header *)
378 ((unsigned long) pcct_entry + pcct_entry->length);
379 }
380
381 pcc_mbox_ctrl.num_chans = count;
382
383 pr_info("Detected %d PCC Subspaces\n", pcc_mbox_ctrl.num_chans);
384
385 return 0;
386 }
387
388 /**
389 * pcc_mbox_probe - Called when we find a match for the
390 * PCCT platform device. This is purely used to represent
391 * the PCCT as a virtual device for registering with the
392 * generic Mailbox framework.
393 *
394 * @pdev: Pointer to platform device returned when a match
395 * is found.
396 *
397 * Return: 0 for Success, else errno.
398 */
399 static int pcc_mbox_probe(struct platform_device *pdev)
400 {
401 int ret = 0;
402
403 pcc_mbox_ctrl.chans = pcc_mbox_channels;
404 pcc_mbox_ctrl.ops = &pcc_chan_ops;
405 pcc_mbox_ctrl.dev = &pdev->dev;
406
407 pr_info("Registering PCC driver as Mailbox controller\n");
408 ret = mbox_controller_register(&pcc_mbox_ctrl);
409
410 if (ret) {
411 pr_err("Err registering PCC as Mailbox controller: %d\n", ret);
412 ret = -ENODEV;
413 }
414
415 return ret;
416 }
417
418 struct platform_driver pcc_mbox_driver = {
419 .probe = pcc_mbox_probe,
420 .driver = {
421 .name = "PCCT",
422 .owner = THIS_MODULE,
423 },
424 };
425
426 static int __init pcc_init(void)
427 {
428 int ret;
429 struct platform_device *pcc_pdev;
430
431 if (acpi_disabled)
432 return -ENODEV;
433
434 /* Check if PCC support is available. */
435 ret = acpi_pcc_probe();
436
437 if (ret) {
438 pr_debug("ACPI PCC probe failed.\n");
439 return -ENODEV;
440 }
441
442 pcc_pdev = platform_create_bundle(&pcc_mbox_driver,
443 pcc_mbox_probe, NULL, 0, NULL, 0);
444
445 if (IS_ERR(pcc_pdev)) {
446 pr_debug("Err creating PCC platform bundle\n");
447 return PTR_ERR(pcc_pdev);
448 }
449
450 return 0;
451 }
452
453 /*
454 * Make PCC init postcore so that users of this mailbox
455 * such as the ACPI Processor driver have it available
456 * at their init.
457 */
458 postcore_initcall(pcc_init);