1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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/interrupt.h>
63#include <linux/list.h>
64#include <linux/platform_device.h>
65#include <linux/mailbox_controller.h>
66#include <linux/mailbox_client.h>
67#include <linux/io-64-nonatomic-lo-hi.h>
68#include <acpi/pcc.h>
69
70#include "mailbox.h"
71
72#define MBOX_IRQ_NAME "pcc-mbox"
73
74static struct mbox_chan *pcc_mbox_channels;
75
76
77static void __iomem **pcc_doorbell_vaddr;
78
79static void __iomem **pcc_doorbell_ack_vaddr;
80
81static int *pcc_doorbell_irq;
82
83static struct mbox_controller pcc_mbox_ctrl = {};
84
85
86
87
88
89
90
91
92static struct mbox_chan *get_pcc_channel(int id)
93{
94 if (id < 0 || id >= pcc_mbox_ctrl.num_chans)
95 return ERR_PTR(-ENOENT);
96
97 return &pcc_mbox_channels[id];
98}
99
100
101
102
103
104
105
106
107
108static int read_register(void __iomem *vaddr, u64 *val, unsigned int bit_width)
109{
110 int ret_val = 0;
111
112 switch (bit_width) {
113 case 8:
114 *val = readb(vaddr);
115 break;
116 case 16:
117 *val = readw(vaddr);
118 break;
119 case 32:
120 *val = readl(vaddr);
121 break;
122 case 64:
123 *val = readq(vaddr);
124 break;
125 default:
126 pr_debug("Error: Cannot read register of %u bit width",
127 bit_width);
128 ret_val = -EFAULT;
129 break;
130 }
131 return ret_val;
132}
133
134static int write_register(void __iomem *vaddr, u64 val, unsigned int bit_width)
135{
136 int ret_val = 0;
137
138 switch (bit_width) {
139 case 8:
140 writeb(val, vaddr);
141 break;
142 case 16:
143 writew(val, vaddr);
144 break;
145 case 32:
146 writel(val, vaddr);
147 break;
148 case 64:
149 writeq(val, vaddr);
150 break;
151 default:
152 pr_debug("Error: Cannot write register of %u bit width",
153 bit_width);
154 ret_val = -EFAULT;
155 break;
156 }
157 return ret_val;
158}
159
160
161
162
163
164
165
166
167
168static int pcc_map_interrupt(u32 interrupt, u32 flags)
169{
170 int trigger, polarity;
171
172 if (!interrupt)
173 return 0;
174
175 trigger = (flags & ACPI_PCCT_INTERRUPT_MODE) ? ACPI_EDGE_SENSITIVE
176 : ACPI_LEVEL_SENSITIVE;
177
178 polarity = (flags & ACPI_PCCT_INTERRUPT_POLARITY) ? ACPI_ACTIVE_LOW
179 : ACPI_ACTIVE_HIGH;
180
181 return acpi_register_gsi(NULL, interrupt, trigger, polarity);
182}
183
184
185
186
187static irqreturn_t pcc_mbox_irq(int irq, void *p)
188{
189 struct acpi_generic_address *doorbell_ack;
190 struct acpi_pcct_hw_reduced *pcct_ss;
191 struct mbox_chan *chan = p;
192 u64 doorbell_ack_preserve;
193 u64 doorbell_ack_write;
194 u64 doorbell_ack_val;
195 int ret;
196
197 pcct_ss = chan->con_priv;
198
199 mbox_chan_received_data(chan, NULL);
200
201 if (pcct_ss->header.type == ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE_TYPE2) {
202 struct acpi_pcct_hw_reduced_type2 *pcct2_ss = chan->con_priv;
203 u32 id = chan - pcc_mbox_channels;
204
205 doorbell_ack = &pcct2_ss->platform_ack_register;
206 doorbell_ack_preserve = pcct2_ss->ack_preserve_mask;
207 doorbell_ack_write = pcct2_ss->ack_write_mask;
208
209 ret = read_register(pcc_doorbell_ack_vaddr[id],
210 &doorbell_ack_val,
211 doorbell_ack->bit_width);
212 if (ret)
213 return IRQ_NONE;
214
215 ret = write_register(pcc_doorbell_ack_vaddr[id],
216 (doorbell_ack_val & doorbell_ack_preserve)
217 | doorbell_ack_write,
218 doorbell_ack->bit_width);
219 if (ret)
220 return IRQ_NONE;
221 }
222
223 return IRQ_HANDLED;
224}
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239struct mbox_chan *pcc_mbox_request_channel(struct mbox_client *cl,
240 int subspace_id)
241{
242 struct device *dev = pcc_mbox_ctrl.dev;
243 struct mbox_chan *chan;
244 unsigned long flags;
245
246
247
248
249
250
251
252
253 chan = get_pcc_channel(subspace_id);
254
255 if (IS_ERR(chan) || chan->cl) {
256 dev_err(dev, "Channel not found for idx: %d\n", subspace_id);
257 return ERR_PTR(-EBUSY);
258 }
259
260 spin_lock_irqsave(&chan->lock, flags);
261 chan->msg_free = 0;
262 chan->msg_count = 0;
263 chan->active_req = NULL;
264 chan->cl = cl;
265 init_completion(&chan->tx_complete);
266
267 if (chan->txdone_method == TXDONE_BY_POLL && cl->knows_txdone)
268 chan->txdone_method = TXDONE_BY_ACK;
269
270 spin_unlock_irqrestore(&chan->lock, flags);
271
272 if (pcc_doorbell_irq[subspace_id] > 0) {
273 int rc;
274
275 rc = devm_request_irq(dev, pcc_doorbell_irq[subspace_id],
276 pcc_mbox_irq, 0, MBOX_IRQ_NAME, chan);
277 if (unlikely(rc)) {
278 dev_err(dev, "failed to register PCC interrupt %d\n",
279 pcc_doorbell_irq[subspace_id]);
280 pcc_mbox_free_channel(chan);
281 chan = ERR_PTR(rc);
282 }
283 }
284
285 return chan;
286}
287EXPORT_SYMBOL_GPL(pcc_mbox_request_channel);
288
289
290
291
292
293
294
295void pcc_mbox_free_channel(struct mbox_chan *chan)
296{
297 u32 id = chan - pcc_mbox_channels;
298 unsigned long flags;
299
300 if (!chan || !chan->cl)
301 return;
302
303 if (id >= pcc_mbox_ctrl.num_chans) {
304 pr_debug("pcc_mbox_free_channel: Invalid mbox_chan passed\n");
305 return;
306 }
307
308 if (pcc_doorbell_irq[id] > 0)
309 devm_free_irq(chan->mbox->dev, pcc_doorbell_irq[id], chan);
310
311 spin_lock_irqsave(&chan->lock, flags);
312 chan->cl = NULL;
313 chan->active_req = NULL;
314 if (chan->txdone_method == TXDONE_BY_ACK)
315 chan->txdone_method = TXDONE_BY_POLL;
316
317 spin_unlock_irqrestore(&chan->lock, flags);
318}
319EXPORT_SYMBOL_GPL(pcc_mbox_free_channel);
320
321
322
323
324
325
326
327
328
329
330
331
332
333static int pcc_send_data(struct mbox_chan *chan, void *data)
334{
335 struct acpi_pcct_hw_reduced *pcct_ss = chan->con_priv;
336 struct acpi_generic_address *doorbell;
337 u64 doorbell_preserve;
338 u64 doorbell_val;
339 u64 doorbell_write;
340 u32 id = chan - pcc_mbox_channels;
341 int ret = 0;
342
343 if (id >= pcc_mbox_ctrl.num_chans) {
344 pr_debug("pcc_send_data: Invalid mbox_chan passed\n");
345 return -ENOENT;
346 }
347
348 doorbell = &pcct_ss->doorbell_register;
349 doorbell_preserve = pcct_ss->preserve_mask;
350 doorbell_write = pcct_ss->write_mask;
351
352
353 if (pcc_doorbell_vaddr[id]) {
354 ret = read_register(pcc_doorbell_vaddr[id], &doorbell_val,
355 doorbell->bit_width);
356 if (ret)
357 return ret;
358 ret = write_register(pcc_doorbell_vaddr[id],
359 (doorbell_val & doorbell_preserve) | doorbell_write,
360 doorbell->bit_width);
361 } else {
362 ret = acpi_read(&doorbell_val, doorbell);
363 if (ret)
364 return ret;
365 ret = acpi_write((doorbell_val & doorbell_preserve) | doorbell_write,
366 doorbell);
367 }
368 return ret;
369}
370
371static const struct mbox_chan_ops pcc_chan_ops = {
372 .send_data = pcc_send_data,
373};
374
375
376
377
378
379
380
381
382
383
384
385static int parse_pcc_subspace(struct acpi_subtable_header *header,
386 const unsigned long end)
387{
388 struct acpi_pcct_subspace *ss = (struct acpi_pcct_subspace *) header;
389
390 if (ss->header.type < ACPI_PCCT_TYPE_RESERVED)
391 return 0;
392
393 return -EINVAL;
394}
395
396
397
398
399
400
401
402
403
404
405
406static int pcc_parse_subspace_irq(int id,
407 struct acpi_pcct_hw_reduced *pcct_ss)
408{
409 pcc_doorbell_irq[id] = pcc_map_interrupt(pcct_ss->platform_interrupt,
410 (u32)pcct_ss->flags);
411 if (pcc_doorbell_irq[id] <= 0) {
412 pr_err("PCC GSI %d not registered\n",
413 pcct_ss->platform_interrupt);
414 return -EINVAL;
415 }
416
417 if (pcct_ss->header.type
418 == ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE_TYPE2) {
419 struct acpi_pcct_hw_reduced_type2 *pcct2_ss = (void *)pcct_ss;
420
421 pcc_doorbell_ack_vaddr[id] = acpi_os_ioremap(
422 pcct2_ss->platform_ack_register.address,
423 pcct2_ss->platform_ack_register.bit_width / 8);
424 if (!pcc_doorbell_ack_vaddr[id]) {
425 pr_err("Failed to ioremap PCC ACK register\n");
426 return -ENOMEM;
427 }
428 }
429
430 return 0;
431}
432
433
434
435
436
437
438static int __init acpi_pcc_probe(void)
439{
440 struct acpi_table_header *pcct_tbl;
441 struct acpi_subtable_header *pcct_entry;
442 struct acpi_table_pcct *acpi_pcct_tbl;
443 struct acpi_subtable_proc proc[ACPI_PCCT_TYPE_RESERVED];
444 int count, i, rc;
445 acpi_status status = AE_OK;
446
447
448 status = acpi_get_table(ACPI_SIG_PCCT, 0, &pcct_tbl);
449
450 if (ACPI_FAILURE(status) || !pcct_tbl)
451 return -ENODEV;
452
453
454 for (i = ACPI_PCCT_TYPE_GENERIC_SUBSPACE;
455 i < ACPI_PCCT_TYPE_RESERVED; i++) {
456 proc[i].id = i;
457 proc[i].count = 0;
458 proc[i].handler = parse_pcc_subspace;
459 }
460
461 count = acpi_table_parse_entries_array(ACPI_SIG_PCCT,
462 sizeof(struct acpi_table_pcct), proc,
463 ACPI_PCCT_TYPE_RESERVED, MAX_PCC_SUBSPACES);
464 if (count == 0 || count > MAX_PCC_SUBSPACES) {
465 pr_warn("Invalid PCCT: %d PCC subspaces\n", count);
466 return -EINVAL;
467 }
468
469 pcc_mbox_channels = kcalloc(count, sizeof(struct mbox_chan),
470 GFP_KERNEL);
471 if (!pcc_mbox_channels) {
472 pr_err("Could not allocate space for PCC mbox channels\n");
473 return -ENOMEM;
474 }
475
476 pcc_doorbell_vaddr = kcalloc(count, sizeof(void *), GFP_KERNEL);
477 if (!pcc_doorbell_vaddr) {
478 rc = -ENOMEM;
479 goto err_free_mbox;
480 }
481
482 pcc_doorbell_ack_vaddr = kcalloc(count, sizeof(void *), GFP_KERNEL);
483 if (!pcc_doorbell_ack_vaddr) {
484 rc = -ENOMEM;
485 goto err_free_db_vaddr;
486 }
487
488 pcc_doorbell_irq = kcalloc(count, sizeof(int), GFP_KERNEL);
489 if (!pcc_doorbell_irq) {
490 rc = -ENOMEM;
491 goto err_free_db_ack_vaddr;
492 }
493
494
495 pcct_entry = (struct acpi_subtable_header *) (
496 (unsigned long) pcct_tbl + sizeof(struct acpi_table_pcct));
497
498 acpi_pcct_tbl = (struct acpi_table_pcct *) pcct_tbl;
499 if (acpi_pcct_tbl->flags & ACPI_PCCT_DOORBELL)
500 pcc_mbox_ctrl.txdone_irq = true;
501
502 for (i = 0; i < count; i++) {
503 struct acpi_generic_address *db_reg;
504 struct acpi_pcct_subspace *pcct_ss;
505 pcc_mbox_channels[i].con_priv = pcct_entry;
506
507 if (pcct_entry->type == ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE ||
508 pcct_entry->type == ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE_TYPE2) {
509 struct acpi_pcct_hw_reduced *pcct_hrss;
510
511 pcct_hrss = (struct acpi_pcct_hw_reduced *) pcct_entry;
512
513 if (pcc_mbox_ctrl.txdone_irq) {
514 rc = pcc_parse_subspace_irq(i, pcct_hrss);
515 if (rc < 0)
516 goto err;
517 }
518 }
519 pcct_ss = (struct acpi_pcct_subspace *) pcct_entry;
520
521
522 db_reg = &pcct_ss->doorbell_register;
523 if (db_reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
524 pcc_doorbell_vaddr[i] = acpi_os_ioremap(db_reg->address,
525 db_reg->bit_width/8);
526 pcct_entry = (struct acpi_subtable_header *)
527 ((unsigned long) pcct_entry + pcct_entry->length);
528 }
529
530 pcc_mbox_ctrl.num_chans = count;
531
532 pr_info("Detected %d PCC Subspaces\n", pcc_mbox_ctrl.num_chans);
533
534 return 0;
535
536err:
537 kfree(pcc_doorbell_irq);
538err_free_db_ack_vaddr:
539 kfree(pcc_doorbell_ack_vaddr);
540err_free_db_vaddr:
541 kfree(pcc_doorbell_vaddr);
542err_free_mbox:
543 kfree(pcc_mbox_channels);
544 return rc;
545}
546
547
548
549
550
551
552
553
554
555
556
557
558static int pcc_mbox_probe(struct platform_device *pdev)
559{
560 int ret = 0;
561
562 pcc_mbox_ctrl.chans = pcc_mbox_channels;
563 pcc_mbox_ctrl.ops = &pcc_chan_ops;
564 pcc_mbox_ctrl.dev = &pdev->dev;
565
566 pr_info("Registering PCC driver as Mailbox controller\n");
567 ret = mbox_controller_register(&pcc_mbox_ctrl);
568
569 if (ret) {
570 pr_err("Err registering PCC as Mailbox controller: %d\n", ret);
571 ret = -ENODEV;
572 }
573
574 return ret;
575}
576
577struct platform_driver pcc_mbox_driver = {
578 .probe = pcc_mbox_probe,
579 .driver = {
580 .name = "PCCT",
581 .owner = THIS_MODULE,
582 },
583};
584
585static int __init pcc_init(void)
586{
587 int ret;
588 struct platform_device *pcc_pdev;
589
590 if (acpi_disabled)
591 return -ENODEV;
592
593
594 ret = acpi_pcc_probe();
595
596 if (ret) {
597 pr_debug("ACPI PCC probe failed.\n");
598 return -ENODEV;
599 }
600
601 pcc_pdev = platform_create_bundle(&pcc_mbox_driver,
602 pcc_mbox_probe, NULL, 0, NULL, 0);
603
604 if (IS_ERR(pcc_pdev)) {
605 pr_debug("Err creating PCC platform bundle\n");
606 return PTR_ERR(pcc_pdev);
607 }
608
609 return 0;
610}
611
612
613
614
615
616
617postcore_initcall(pcc_init);
618