1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * RISC-V SBI Message Proxy (MPXY) mailbox controller driver
4 *
5 * Copyright (C) 2025 Ventana Micro Systems Inc.
6 */
7
8 #include <linux/acpi.h>
9 #include <linux/cpu.h>
10 #include <linux/errno.h>
11 #include <linux/init.h>
12 #include <linux/irqchip/riscv-imsic.h>
13 #include <linux/mailbox_controller.h>
14 #include <linux/mailbox/riscv-rpmi-message.h>
15 #include <linux/minmax.h>
16 #include <linux/mm.h>
17 #include <linux/module.h>
18 #include <linux/msi.h>
19 #include <linux/of_irq.h>
20 #include <linux/percpu.h>
21 #include <linux/platform_device.h>
22 #include <linux/smp.h>
23 #include <linux/string.h>
24 #include <linux/types.h>
25 #include <asm/byteorder.h>
26 #include <asm/sbi.h>
27
28 /* ====== SBI MPXY extension data structures ====== */
29
30 /* SBI MPXY MSI related channel attributes */
31 struct sbi_mpxy_msi_info {
32 /* Lower 32-bits of the MSI target address */
33 u32 msi_addr_lo;
34 /* Upper 32-bits of the MSI target address */
35 u32 msi_addr_hi;
36 /* MSI data value */
37 u32 msi_data;
38 };
39
40 /*
41 * SBI MPXY standard channel attributes.
42 *
43 * NOTE: The sequence of attribute fields are as-per the
44 * defined sequence in the attribute table in spec (or
45 * as-per the enum sbi_mpxy_attribute_id).
46 */
47 struct sbi_mpxy_channel_attrs {
48 /* Message protocol ID */
49 u32 msg_proto_id;
50 /* Message protocol version */
51 u32 msg_proto_version;
52 /* Message protocol maximum message length */
53 u32 msg_max_len;
54 /* Message protocol message send timeout in microseconds */
55 u32 msg_send_timeout;
56 /* Message protocol message completion timeout in microseconds */
57 u32 msg_completion_timeout;
58 /* Bit array for channel capabilities */
59 u32 capability;
60 /* SSE event ID */
61 u32 sse_event_id;
62 /* MSI enable/disable control knob */
63 u32 msi_control;
64 /* Channel MSI info */
65 struct sbi_mpxy_msi_info msi_info;
66 /* Events state control */
67 u32 events_state_ctrl;
68 };
69
70 /*
71 * RPMI specific SBI MPXY channel attributes.
72 *
73 * NOTE: The sequence of attribute fields are as-per the
74 * defined sequence in the attribute table in spec (or
75 * as-per the enum sbi_mpxy_rpmi_attribute_id).
76 */
77 struct sbi_mpxy_rpmi_channel_attrs {
78 /* RPMI service group ID */
79 u32 servicegroup_id;
80 /* RPMI service group version */
81 u32 servicegroup_version;
82 /* RPMI implementation ID */
83 u32 impl_id;
84 /* RPMI implementation version */
85 u32 impl_version;
86 };
87
88 /* SBI MPXY channel IDs data in shared memory */
89 struct sbi_mpxy_channel_ids_data {
90 /* Remaining number of channel ids */
91 __le32 remaining;
92 /* Returned channel ids in current function call */
93 __le32 returned;
94 /* Returned channel id array */
95 __le32 channel_array[];
96 };
97
98 /* SBI MPXY notification data in shared memory */
99 struct sbi_mpxy_notification_data {
100 /* Remaining number of notification events */
101 __le32 remaining;
102 /* Number of notification events returned */
103 __le32 returned;
104 /* Number of notification events lost */
105 __le32 lost;
106 /* Reserved for future use */
107 __le32 reserved;
108 /* Returned channel id array */
109 u8 events_data[];
110 };
111
112 /* ====== MPXY data structures & helper routines ====== */
113
114 /* MPXY Per-CPU or local context */
115 struct mpxy_local {
116 /* Shared memory base address */
117 void *shmem;
118 /* Shared memory physical address */
119 phys_addr_t shmem_phys_addr;
120 /* Flag representing whether shared memory is active or not */
121 bool shmem_active;
122 };
123
124 static DEFINE_PER_CPU(struct mpxy_local, mpxy_local);
125 static unsigned long mpxy_shmem_size;
126 static bool mpxy_shmem_init_done;
127
mpxy_get_channel_count(u32 * channel_count)128 static int mpxy_get_channel_count(u32 *channel_count)
129 {
130 struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local);
131 struct sbi_mpxy_channel_ids_data *sdata = mpxy->shmem;
132 u32 remaining, returned;
133 struct sbiret sret;
134
135 if (!mpxy->shmem_active)
136 return -ENODEV;
137 if (!channel_count)
138 return -EINVAL;
139
140 get_cpu();
141
142 /* Get the remaining and returned fields to calculate total */
143 sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_GET_CHANNEL_IDS,
144 0, 0, 0, 0, 0, 0);
145 if (sret.error)
146 goto err_put_cpu;
147
148 remaining = le32_to_cpu(sdata->remaining);
149 returned = le32_to_cpu(sdata->returned);
150 *channel_count = remaining + returned;
151
152 err_put_cpu:
153 put_cpu();
154 return sbi_err_map_linux_errno(sret.error);
155 }
156
mpxy_get_channel_ids(u32 channel_count,u32 * channel_ids)157 static int mpxy_get_channel_ids(u32 channel_count, u32 *channel_ids)
158 {
159 struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local);
160 struct sbi_mpxy_channel_ids_data *sdata = mpxy->shmem;
161 u32 remaining, returned, count, start_index = 0;
162 struct sbiret sret;
163
164 if (!mpxy->shmem_active)
165 return -ENODEV;
166 if (!channel_count || !channel_ids)
167 return -EINVAL;
168
169 get_cpu();
170
171 do {
172 sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_GET_CHANNEL_IDS,
173 start_index, 0, 0, 0, 0, 0);
174 if (sret.error)
175 goto err_put_cpu;
176
177 remaining = le32_to_cpu(sdata->remaining);
178 returned = le32_to_cpu(sdata->returned);
179
180 count = returned < (channel_count - start_index) ?
181 returned : (channel_count - start_index);
182 memcpy_from_le32(&channel_ids[start_index], sdata->channel_array, count);
183 start_index += count;
184 } while (remaining && start_index < channel_count);
185
186 err_put_cpu:
187 put_cpu();
188 return sbi_err_map_linux_errno(sret.error);
189 }
190
mpxy_read_attrs(u32 channel_id,u32 base_attrid,u32 attr_count,u32 * attrs_buf)191 static int mpxy_read_attrs(u32 channel_id, u32 base_attrid, u32 attr_count,
192 u32 *attrs_buf)
193 {
194 struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local);
195 struct sbiret sret;
196
197 if (!mpxy->shmem_active)
198 return -ENODEV;
199 if (!attr_count || !attrs_buf)
200 return -EINVAL;
201
202 get_cpu();
203
204 sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_READ_ATTRS,
205 channel_id, base_attrid, attr_count, 0, 0, 0);
206 if (sret.error)
207 goto err_put_cpu;
208
209 memcpy_from_le32(attrs_buf, (__le32 *)mpxy->shmem, attr_count);
210
211 err_put_cpu:
212 put_cpu();
213 return sbi_err_map_linux_errno(sret.error);
214 }
215
mpxy_write_attrs(u32 channel_id,u32 base_attrid,u32 attr_count,u32 * attrs_buf)216 static int mpxy_write_attrs(u32 channel_id, u32 base_attrid, u32 attr_count,
217 u32 *attrs_buf)
218 {
219 struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local);
220 struct sbiret sret;
221
222 if (!mpxy->shmem_active)
223 return -ENODEV;
224 if (!attr_count || !attrs_buf)
225 return -EINVAL;
226
227 get_cpu();
228
229 memcpy_to_le32((__le32 *)mpxy->shmem, attrs_buf, attr_count);
230 sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_WRITE_ATTRS,
231 channel_id, base_attrid, attr_count, 0, 0, 0);
232
233 put_cpu();
234 return sbi_err_map_linux_errno(sret.error);
235 }
236
mpxy_send_message_with_resp(u32 channel_id,u32 msg_id,void * tx,unsigned long tx_len,void * rx,unsigned long max_rx_len,unsigned long * rx_len)237 static int mpxy_send_message_with_resp(u32 channel_id, u32 msg_id,
238 void *tx, unsigned long tx_len,
239 void *rx, unsigned long max_rx_len,
240 unsigned long *rx_len)
241 {
242 struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local);
243 unsigned long rx_bytes;
244 struct sbiret sret;
245
246 if (!mpxy->shmem_active)
247 return -ENODEV;
248 if (!tx && tx_len)
249 return -EINVAL;
250
251 get_cpu();
252
253 /* Message protocols allowed to have no data in messages */
254 if (tx_len)
255 memcpy(mpxy->shmem, tx, tx_len);
256
257 sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_SEND_MSG_WITH_RESP,
258 channel_id, msg_id, tx_len, 0, 0, 0);
259 if (rx && !sret.error) {
260 rx_bytes = sret.value;
261 if (rx_bytes > max_rx_len) {
262 put_cpu();
263 return -ENOSPC;
264 }
265
266 memcpy(rx, mpxy->shmem, rx_bytes);
267 if (rx_len)
268 *rx_len = rx_bytes;
269 }
270
271 put_cpu();
272 return sbi_err_map_linux_errno(sret.error);
273 }
274
mpxy_send_message_without_resp(u32 channel_id,u32 msg_id,void * tx,unsigned long tx_len)275 static int mpxy_send_message_without_resp(u32 channel_id, u32 msg_id,
276 void *tx, unsigned long tx_len)
277 {
278 struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local);
279 struct sbiret sret;
280
281 if (!mpxy->shmem_active)
282 return -ENODEV;
283 if (!tx && tx_len)
284 return -EINVAL;
285
286 get_cpu();
287
288 /* Message protocols allowed to have no data in messages */
289 if (tx_len)
290 memcpy(mpxy->shmem, tx, tx_len);
291
292 sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_SEND_MSG_WITHOUT_RESP,
293 channel_id, msg_id, tx_len, 0, 0, 0);
294
295 put_cpu();
296 return sbi_err_map_linux_errno(sret.error);
297 }
298
mpxy_get_notifications(u32 channel_id,struct sbi_mpxy_notification_data * notif_data,unsigned long * events_data_len)299 static int mpxy_get_notifications(u32 channel_id,
300 struct sbi_mpxy_notification_data *notif_data,
301 unsigned long *events_data_len)
302 {
303 struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local);
304 struct sbiret sret;
305
306 if (!mpxy->shmem_active)
307 return -ENODEV;
308 if (!notif_data || !events_data_len)
309 return -EINVAL;
310
311 get_cpu();
312
313 sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_GET_NOTIFICATION_EVENTS,
314 channel_id, 0, 0, 0, 0, 0);
315 if (sret.error)
316 goto err_put_cpu;
317 if (sret.value < 0 || mpxy_shmem_size < sizeof(*notif_data) ||
318 sret.value > mpxy_shmem_size - sizeof(*notif_data)) {
319 put_cpu();
320 return -EOVERFLOW;
321 }
322
323 memcpy(notif_data, mpxy->shmem, sret.value + sizeof(*notif_data));
324 *events_data_len = sret.value;
325
326 err_put_cpu:
327 put_cpu();
328 return sbi_err_map_linux_errno(sret.error);
329 }
330
mpxy_get_shmem_size(unsigned long * shmem_size)331 static int mpxy_get_shmem_size(unsigned long *shmem_size)
332 {
333 struct sbiret sret;
334
335 sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_GET_SHMEM_SIZE,
336 0, 0, 0, 0, 0, 0);
337 if (sret.error)
338 return sbi_err_map_linux_errno(sret.error);
339 if (shmem_size)
340 *shmem_size = sret.value;
341 return 0;
342 }
343
mpxy_setup_shmem(unsigned int cpu)344 static int mpxy_setup_shmem(unsigned int cpu)
345 {
346 struct page *shmem_page;
347 struct mpxy_local *mpxy;
348 struct sbiret sret;
349
350 mpxy = per_cpu_ptr(&mpxy_local, cpu);
351 if (mpxy->shmem_active)
352 return 0;
353
354 shmem_page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(mpxy_shmem_size));
355 if (!shmem_page)
356 return -ENOMEM;
357
358 /*
359 * Linux setup of shmem is done in mpxy OVERWRITE mode.
360 * flags[1:0] = 00b
361 */
362 sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_SET_SHMEM,
363 page_to_phys(shmem_page), 0, 0, 0, 0, 0);
364 if (sret.error) {
365 free_pages((unsigned long)page_to_virt(shmem_page),
366 get_order(mpxy_shmem_size));
367 return sbi_err_map_linux_errno(sret.error);
368 }
369
370 mpxy->shmem = page_to_virt(shmem_page);
371 mpxy->shmem_phys_addr = page_to_phys(shmem_page);
372 mpxy->shmem_active = true;
373
374 return 0;
375 }
376
377 /* ====== MPXY mailbox data structures ====== */
378
379 /* MPXY mailbox channel */
380 struct mpxy_mbox_channel {
381 struct mpxy_mbox *mbox;
382 u32 channel_id;
383 struct sbi_mpxy_channel_attrs attrs;
384 struct sbi_mpxy_rpmi_channel_attrs rpmi_attrs;
385 struct sbi_mpxy_notification_data *notif;
386 u32 max_xfer_len;
387 bool have_events_state;
388 u32 msi_index;
389 u32 msi_irq;
390 bool started;
391 };
392
393 /* MPXY mailbox */
394 struct mpxy_mbox {
395 struct device *dev;
396 u32 channel_count;
397 struct mpxy_mbox_channel *channels;
398 u32 msi_count;
399 struct mpxy_mbox_channel **msi_index_to_channel;
400 struct mbox_controller controller;
401 };
402
403 /* ====== MPXY RPMI processing ====== */
404
mpxy_mbox_send_rpmi_data(struct mpxy_mbox_channel * mchan,struct rpmi_mbox_message * msg)405 static void mpxy_mbox_send_rpmi_data(struct mpxy_mbox_channel *mchan,
406 struct rpmi_mbox_message *msg)
407 {
408 msg->error = 0;
409 switch (msg->type) {
410 case RPMI_MBOX_MSG_TYPE_GET_ATTRIBUTE:
411 switch (msg->attr.id) {
412 case RPMI_MBOX_ATTR_SPEC_VERSION:
413 msg->attr.value = mchan->attrs.msg_proto_version;
414 break;
415 case RPMI_MBOX_ATTR_MAX_MSG_DATA_SIZE:
416 msg->attr.value = mchan->max_xfer_len;
417 break;
418 case RPMI_MBOX_ATTR_SERVICEGROUP_ID:
419 msg->attr.value = mchan->rpmi_attrs.servicegroup_id;
420 break;
421 case RPMI_MBOX_ATTR_SERVICEGROUP_VERSION:
422 msg->attr.value = mchan->rpmi_attrs.servicegroup_version;
423 break;
424 case RPMI_MBOX_ATTR_IMPL_ID:
425 msg->attr.value = mchan->rpmi_attrs.impl_id;
426 break;
427 case RPMI_MBOX_ATTR_IMPL_VERSION:
428 msg->attr.value = mchan->rpmi_attrs.impl_version;
429 break;
430 default:
431 msg->error = -EOPNOTSUPP;
432 break;
433 }
434 break;
435 case RPMI_MBOX_MSG_TYPE_SET_ATTRIBUTE:
436 /* None of the RPMI linux mailbox attributes are writeable */
437 msg->error = -EOPNOTSUPP;
438 break;
439 case RPMI_MBOX_MSG_TYPE_SEND_WITH_RESPONSE:
440 if ((!msg->data.request && msg->data.request_len) ||
441 (msg->data.request && msg->data.request_len > mchan->max_xfer_len) ||
442 (!msg->data.response && msg->data.max_response_len)) {
443 msg->error = -EINVAL;
444 break;
445 }
446 if (!(mchan->attrs.capability & SBI_MPXY_CHAN_CAP_SEND_WITH_RESP)) {
447 msg->error = -EIO;
448 break;
449 }
450 msg->error = mpxy_send_message_with_resp(mchan->channel_id,
451 msg->data.service_id,
452 msg->data.request,
453 msg->data.request_len,
454 msg->data.response,
455 msg->data.max_response_len,
456 &msg->data.out_response_len);
457 break;
458 case RPMI_MBOX_MSG_TYPE_SEND_WITHOUT_RESPONSE:
459 if ((!msg->data.request && msg->data.request_len) ||
460 (msg->data.request && msg->data.request_len > mchan->max_xfer_len)) {
461 msg->error = -EINVAL;
462 break;
463 }
464 if (!(mchan->attrs.capability & SBI_MPXY_CHAN_CAP_SEND_WITHOUT_RESP)) {
465 msg->error = -EIO;
466 break;
467 }
468 msg->error = mpxy_send_message_without_resp(mchan->channel_id,
469 msg->data.service_id,
470 msg->data.request,
471 msg->data.request_len);
472 break;
473 default:
474 msg->error = -EOPNOTSUPP;
475 break;
476 }
477 }
478
mpxy_mbox_peek_rpmi_data(struct mbox_chan * chan,struct mpxy_mbox_channel * mchan,struct sbi_mpxy_notification_data * notif,unsigned long events_data_len)479 static void mpxy_mbox_peek_rpmi_data(struct mbox_chan *chan,
480 struct mpxy_mbox_channel *mchan,
481 struct sbi_mpxy_notification_data *notif,
482 unsigned long events_data_len)
483 {
484 struct rpmi_notification_event *event;
485 struct rpmi_mbox_message msg;
486 unsigned long pos = 0;
487
488 while (events_data_len - pos >= sizeof(*event)) {
489 event = (struct rpmi_notification_event *)(notif->events_data + pos);
490
491 msg.type = RPMI_MBOX_MSG_TYPE_NOTIFICATION_EVENT;
492 msg.notif.event_datalen = le16_to_cpu(event->event_datalen);
493 if (msg.notif.event_datalen >
494 events_data_len - pos - sizeof(*event))
495 break;
496 msg.notif.event_id = event->event_id;
497 msg.notif.event_data = event->event_data;
498 msg.error = 0;
499
500 mbox_chan_received_data(chan, &msg);
501 pos += sizeof(*event) + msg.notif.event_datalen;
502 }
503 }
504
mpxy_mbox_read_rpmi_attrs(struct mpxy_mbox_channel * mchan)505 static int mpxy_mbox_read_rpmi_attrs(struct mpxy_mbox_channel *mchan)
506 {
507 return mpxy_read_attrs(mchan->channel_id,
508 SBI_MPXY_ATTR_MSGPROTO_ATTR_START,
509 sizeof(mchan->rpmi_attrs) / sizeof(u32),
510 (u32 *)&mchan->rpmi_attrs);
511 }
512
513 /* ====== MPXY mailbox callbacks ====== */
514
mpxy_mbox_send_data(struct mbox_chan * chan,void * data)515 static int mpxy_mbox_send_data(struct mbox_chan *chan, void *data)
516 {
517 struct mpxy_mbox_channel *mchan = chan->con_priv;
518
519 if (mchan->attrs.msg_proto_id == SBI_MPXY_MSGPROTO_RPMI_ID) {
520 mpxy_mbox_send_rpmi_data(mchan, data);
521 return 0;
522 }
523
524 return -EOPNOTSUPP;
525 }
526
mpxy_mbox_peek_data(struct mbox_chan * chan)527 static bool mpxy_mbox_peek_data(struct mbox_chan *chan)
528 {
529 struct mpxy_mbox_channel *mchan = chan->con_priv;
530 struct sbi_mpxy_notification_data *notif = mchan->notif;
531 bool have_notifications = false;
532 unsigned long data_len;
533 int rc;
534
535 if (!(mchan->attrs.capability & SBI_MPXY_CHAN_CAP_GET_NOTIFICATIONS))
536 return false;
537
538 do {
539 rc = mpxy_get_notifications(mchan->channel_id, notif, &data_len);
540 if (rc || !data_len)
541 break;
542
543 if (mchan->attrs.msg_proto_id == SBI_MPXY_MSGPROTO_RPMI_ID)
544 mpxy_mbox_peek_rpmi_data(chan, mchan, notif, data_len);
545
546 have_notifications = true;
547 } while (1);
548
549 return have_notifications;
550 }
551
mpxy_mbox_irq_thread(int irq,void * dev_id)552 static irqreturn_t mpxy_mbox_irq_thread(int irq, void *dev_id)
553 {
554 mpxy_mbox_peek_data(dev_id);
555 return IRQ_HANDLED;
556 }
557
mpxy_mbox_setup_msi(struct mbox_chan * chan,struct mpxy_mbox_channel * mchan)558 static int mpxy_mbox_setup_msi(struct mbox_chan *chan,
559 struct mpxy_mbox_channel *mchan)
560 {
561 struct device *dev = mchan->mbox->dev;
562 int rc;
563
564 /* Do nothing if MSI not supported */
565 if (mchan->msi_irq == U32_MAX)
566 return 0;
567
568 /* Fail if MSI already enabled */
569 if (mchan->attrs.msi_control)
570 return -EALREADY;
571
572 /* Request channel MSI handler */
573 rc = request_threaded_irq(mchan->msi_irq, NULL, mpxy_mbox_irq_thread,
574 0, dev_name(dev), chan);
575 if (rc) {
576 dev_err(dev, "failed to request MPXY channel 0x%x IRQ\n",
577 mchan->channel_id);
578 return rc;
579 }
580
581 /* Enable channel MSI control */
582 mchan->attrs.msi_control = 1;
583 rc = mpxy_write_attrs(mchan->channel_id, SBI_MPXY_ATTR_MSI_CONTROL,
584 1, &mchan->attrs.msi_control);
585 if (rc) {
586 dev_err(dev, "enable MSI control failed for MPXY channel 0x%x\n",
587 mchan->channel_id);
588 mchan->attrs.msi_control = 0;
589 free_irq(mchan->msi_irq, chan);
590 return rc;
591 }
592
593 return 0;
594 }
595
mpxy_mbox_cleanup_msi(struct mbox_chan * chan,struct mpxy_mbox_channel * mchan)596 static void mpxy_mbox_cleanup_msi(struct mbox_chan *chan,
597 struct mpxy_mbox_channel *mchan)
598 {
599 struct device *dev = mchan->mbox->dev;
600 int rc;
601
602 /* Do nothing if MSI not supported */
603 if (mchan->msi_irq == U32_MAX)
604 return;
605
606 /* Do nothing if MSI already disabled */
607 if (!mchan->attrs.msi_control)
608 return;
609
610 /* Disable channel MSI control */
611 mchan->attrs.msi_control = 0;
612 rc = mpxy_write_attrs(mchan->channel_id, SBI_MPXY_ATTR_MSI_CONTROL,
613 1, &mchan->attrs.msi_control);
614 if (rc) {
615 dev_err(dev, "disable MSI control failed for MPXY channel 0x%x\n",
616 mchan->channel_id);
617 }
618
619 /* Free channel MSI handler */
620 free_irq(mchan->msi_irq, chan);
621 }
622
mpxy_mbox_setup_events(struct mpxy_mbox_channel * mchan)623 static int mpxy_mbox_setup_events(struct mpxy_mbox_channel *mchan)
624 {
625 struct device *dev = mchan->mbox->dev;
626 int rc;
627
628 /* Do nothing if events state not supported */
629 if (!mchan->have_events_state)
630 return 0;
631
632 /* Fail if events state already enabled */
633 if (mchan->attrs.events_state_ctrl)
634 return -EALREADY;
635
636 /* Enable channel events state */
637 mchan->attrs.events_state_ctrl = 1;
638 rc = mpxy_write_attrs(mchan->channel_id, SBI_MPXY_ATTR_EVENTS_STATE_CONTROL,
639 1, &mchan->attrs.events_state_ctrl);
640 if (rc) {
641 dev_err(dev, "enable events state failed for MPXY channel 0x%x\n",
642 mchan->channel_id);
643 mchan->attrs.events_state_ctrl = 0;
644 return rc;
645 }
646
647 return 0;
648 }
649
mpxy_mbox_cleanup_events(struct mpxy_mbox_channel * mchan)650 static void mpxy_mbox_cleanup_events(struct mpxy_mbox_channel *mchan)
651 {
652 struct device *dev = mchan->mbox->dev;
653 int rc;
654
655 /* Do nothing if events state not supported */
656 if (!mchan->have_events_state)
657 return;
658
659 /* Do nothing if events state already disabled */
660 if (!mchan->attrs.events_state_ctrl)
661 return;
662
663 /* Disable channel events state */
664 mchan->attrs.events_state_ctrl = 0;
665 rc = mpxy_write_attrs(mchan->channel_id, SBI_MPXY_ATTR_EVENTS_STATE_CONTROL,
666 1, &mchan->attrs.events_state_ctrl);
667 if (rc)
668 dev_err(dev, "disable events state failed for MPXY channel 0x%x\n",
669 mchan->channel_id);
670 }
671
mpxy_mbox_startup(struct mbox_chan * chan)672 static int mpxy_mbox_startup(struct mbox_chan *chan)
673 {
674 struct mpxy_mbox_channel *mchan = chan->con_priv;
675 int rc;
676
677 if (mchan->started)
678 return -EALREADY;
679
680 /* Setup channel MSI */
681 rc = mpxy_mbox_setup_msi(chan, mchan);
682 if (rc)
683 return rc;
684
685 /* Setup channel notification events */
686 rc = mpxy_mbox_setup_events(mchan);
687 if (rc) {
688 mpxy_mbox_cleanup_msi(chan, mchan);
689 return rc;
690 }
691
692 /* Mark the channel as started */
693 mchan->started = true;
694
695 return 0;
696 }
697
mpxy_mbox_shutdown(struct mbox_chan * chan)698 static void mpxy_mbox_shutdown(struct mbox_chan *chan)
699 {
700 struct mpxy_mbox_channel *mchan = chan->con_priv;
701
702 if (!mchan->started)
703 return;
704
705 /* Mark the channel as stopped */
706 mchan->started = false;
707
708 /* Cleanup channel notification events */
709 mpxy_mbox_cleanup_events(mchan);
710
711 /* Cleanup channel MSI */
712 mpxy_mbox_cleanup_msi(chan, mchan);
713 }
714
715 static const struct mbox_chan_ops mpxy_mbox_ops = {
716 .send_data = mpxy_mbox_send_data,
717 .peek_data = mpxy_mbox_peek_data,
718 .startup = mpxy_mbox_startup,
719 .shutdown = mpxy_mbox_shutdown,
720 };
721
722 /* ====== MPXY platform driver ===== */
723
mpxy_mbox_msi_write(struct msi_desc * desc,struct msi_msg * msg)724 static void mpxy_mbox_msi_write(struct msi_desc *desc, struct msi_msg *msg)
725 {
726 struct device *dev = msi_desc_to_dev(desc);
727 struct mpxy_mbox *mbox = dev_get_drvdata(dev);
728 struct mpxy_mbox_channel *mchan;
729 struct sbi_mpxy_msi_info *minfo;
730 int rc;
731
732 mchan = mbox->msi_index_to_channel[desc->msi_index];
733 if (!mchan) {
734 dev_warn(dev, "MPXY channel not available for MSI index %d\n",
735 desc->msi_index);
736 return;
737 }
738
739 minfo = &mchan->attrs.msi_info;
740 minfo->msi_addr_lo = msg->address_lo;
741 minfo->msi_addr_hi = msg->address_hi;
742 minfo->msi_data = msg->data;
743
744 rc = mpxy_write_attrs(mchan->channel_id, SBI_MPXY_ATTR_MSI_ADDR_LO,
745 sizeof(*minfo) / sizeof(u32), (u32 *)minfo);
746 if (rc) {
747 dev_warn(dev, "failed to write MSI info for MPXY channel 0x%x\n",
748 mchan->channel_id);
749 }
750 }
751
mpxy_mbox_fw_xlate(struct mbox_controller * ctlr,const struct fwnode_reference_args * pa)752 static struct mbox_chan *mpxy_mbox_fw_xlate(struct mbox_controller *ctlr,
753 const struct fwnode_reference_args *pa)
754 {
755 struct mpxy_mbox *mbox = container_of(ctlr, struct mpxy_mbox, controller);
756 struct mpxy_mbox_channel *mchan;
757 u32 i;
758
759 if (pa->nargs != 2)
760 return ERR_PTR(-EINVAL);
761
762 for (i = 0; i < mbox->channel_count; i++) {
763 mchan = &mbox->channels[i];
764 if (mchan->channel_id == pa->args[0] &&
765 mchan->attrs.msg_proto_id == pa->args[1])
766 return &mbox->controller.chans[i];
767 }
768
769 return ERR_PTR(-ENOENT);
770 }
771
mpxy_mbox_populate_channels(struct mpxy_mbox * mbox)772 static int mpxy_mbox_populate_channels(struct mpxy_mbox *mbox)
773 {
774 u32 i, *channel_ids __free(kfree) = NULL;
775 struct mpxy_mbox_channel *mchan;
776 int rc;
777
778 /* Find-out of number of channels */
779 rc = mpxy_get_channel_count(&mbox->channel_count);
780 if (rc)
781 return dev_err_probe(mbox->dev, rc, "failed to get number of MPXY channels\n");
782 if (!mbox->channel_count)
783 return dev_err_probe(mbox->dev, -ENODEV, "no MPXY channels available\n");
784
785 /* Allocate and fetch all channel IDs */
786 channel_ids = kcalloc(mbox->channel_count, sizeof(*channel_ids), GFP_KERNEL);
787 if (!channel_ids)
788 return -ENOMEM;
789 rc = mpxy_get_channel_ids(mbox->channel_count, channel_ids);
790 if (rc)
791 return dev_err_probe(mbox->dev, rc, "failed to get MPXY channel IDs\n");
792
793 /* Populate all channels */
794 mbox->channels = devm_kcalloc(mbox->dev, mbox->channel_count,
795 sizeof(*mbox->channels), GFP_KERNEL);
796 if (!mbox->channels)
797 return -ENOMEM;
798 for (i = 0; i < mbox->channel_count; i++) {
799 mchan = &mbox->channels[i];
800 mchan->mbox = mbox;
801 mchan->channel_id = channel_ids[i];
802
803 rc = mpxy_read_attrs(mchan->channel_id, SBI_MPXY_ATTR_MSG_PROT_ID,
804 sizeof(mchan->attrs) / sizeof(u32),
805 (u32 *)&mchan->attrs);
806 if (rc) {
807 return dev_err_probe(mbox->dev, rc,
808 "MPXY channel 0x%x read attrs failed\n",
809 mchan->channel_id);
810 }
811
812 if (mchan->attrs.msg_proto_id == SBI_MPXY_MSGPROTO_RPMI_ID) {
813 rc = mpxy_mbox_read_rpmi_attrs(mchan);
814 if (rc) {
815 return dev_err_probe(mbox->dev, rc,
816 "MPXY channel 0x%x read RPMI attrs failed\n",
817 mchan->channel_id);
818 }
819 }
820
821 mchan->notif = devm_kzalloc(mbox->dev, mpxy_shmem_size, GFP_KERNEL);
822 if (!mchan->notif)
823 return -ENOMEM;
824
825 mchan->max_xfer_len = min(mpxy_shmem_size, mchan->attrs.msg_max_len);
826
827 if ((mchan->attrs.capability & SBI_MPXY_CHAN_CAP_GET_NOTIFICATIONS) &&
828 (mchan->attrs.capability & SBI_MPXY_CHAN_CAP_EVENTS_STATE))
829 mchan->have_events_state = true;
830
831 if ((mchan->attrs.capability & SBI_MPXY_CHAN_CAP_GET_NOTIFICATIONS) &&
832 (mchan->attrs.capability & SBI_MPXY_CHAN_CAP_MSI))
833 mchan->msi_index = mbox->msi_count++;
834 else
835 mchan->msi_index = U32_MAX;
836 mchan->msi_irq = U32_MAX;
837 }
838
839 return 0;
840 }
841
mpxy_mbox_probe(struct platform_device * pdev)842 static int mpxy_mbox_probe(struct platform_device *pdev)
843 {
844 struct device *dev = &pdev->dev;
845 struct mpxy_mbox_channel *mchan;
846 struct mpxy_mbox *mbox;
847 int msi_idx, rc;
848 u32 i;
849
850 /*
851 * Initialize MPXY shared memory only once. This also ensures
852 * that SBI MPXY mailbox is probed only once.
853 */
854 if (mpxy_shmem_init_done) {
855 dev_err(dev, "SBI MPXY mailbox already initialized\n");
856 return -EALREADY;
857 }
858
859 /* Probe for SBI MPXY extension */
860 if (sbi_spec_version < sbi_mk_version(1, 0) ||
861 sbi_probe_extension(SBI_EXT_MPXY) <= 0) {
862 dev_info(dev, "SBI MPXY extension not available\n");
863 return -ENODEV;
864 }
865
866 /* Find-out shared memory size */
867 rc = mpxy_get_shmem_size(&mpxy_shmem_size);
868 if (rc)
869 return dev_err_probe(dev, rc, "failed to get MPXY shared memory size\n");
870
871 /*
872 * Setup MPXY shared memory on each CPU
873 *
874 * Note: Don't cleanup MPXY shared memory upon CPU power-down
875 * because the RPMI System MSI irqchip driver needs it to be
876 * available when migrating IRQs in CPU power-down path.
877 */
878 cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "riscv/sbi-mpxy-shmem",
879 mpxy_setup_shmem, NULL);
880
881 /* Mark as MPXY shared memory initialization done */
882 mpxy_shmem_init_done = true;
883
884 /* Allocate mailbox instance */
885 mbox = devm_kzalloc(dev, sizeof(*mbox), GFP_KERNEL);
886 if (!mbox)
887 return -ENOMEM;
888 mbox->dev = dev;
889 platform_set_drvdata(pdev, mbox);
890
891 /* Populate mailbox channels */
892 rc = mpxy_mbox_populate_channels(mbox);
893 if (rc)
894 return rc;
895
896 /* Initialize mailbox controller */
897 mbox->controller.txdone_irq = false;
898 mbox->controller.txdone_poll = false;
899 mbox->controller.ops = &mpxy_mbox_ops;
900 mbox->controller.dev = dev;
901 mbox->controller.num_chans = mbox->channel_count;
902 mbox->controller.fw_xlate = mpxy_mbox_fw_xlate;
903 mbox->controller.chans = devm_kcalloc(dev, mbox->channel_count,
904 sizeof(*mbox->controller.chans),
905 GFP_KERNEL);
906 if (!mbox->controller.chans)
907 return -ENOMEM;
908 for (i = 0; i < mbox->channel_count; i++)
909 mbox->controller.chans[i].con_priv = &mbox->channels[i];
910
911 /* Setup MSIs for mailbox (if required) */
912 if (mbox->msi_count) {
913 /*
914 * The device MSI domain for platform devices on RISC-V architecture
915 * is only available after the MSI controller driver is probed so,
916 * explicitly configure here.
917 */
918 if (!dev_get_msi_domain(dev)) {
919 struct fwnode_handle *fwnode = dev_fwnode(dev);
920
921 /*
922 * The device MSI domain for OF devices is only set at the
923 * time of populating/creating OF device. If the device MSI
924 * domain is discovered later after the OF device is created
925 * then we need to set it explicitly before using any platform
926 * MSI functions.
927 */
928 if (is_of_node(fwnode)) {
929 of_msi_configure(dev, dev_of_node(dev));
930 } else if (is_acpi_device_node(fwnode)) {
931 struct irq_domain *msi_domain;
932
933 msi_domain = irq_find_matching_fwnode(imsic_acpi_get_fwnode(dev),
934 DOMAIN_BUS_PLATFORM_MSI);
935 dev_set_msi_domain(dev, msi_domain);
936 }
937
938 if (!dev_get_msi_domain(dev))
939 return -EPROBE_DEFER;
940 }
941
942 mbox->msi_index_to_channel = devm_kcalloc(dev, mbox->msi_count,
943 sizeof(*mbox->msi_index_to_channel),
944 GFP_KERNEL);
945 if (!mbox->msi_index_to_channel)
946 return -ENOMEM;
947
948 for (msi_idx = 0; msi_idx < mbox->msi_count; msi_idx++) {
949 for (i = 0; i < mbox->channel_count; i++) {
950 mchan = &mbox->channels[i];
951 if (mchan->msi_index == msi_idx) {
952 mbox->msi_index_to_channel[msi_idx] = mchan;
953 break;
954 }
955 }
956 }
957
958 rc = platform_device_msi_init_and_alloc_irqs(dev, mbox->msi_count,
959 mpxy_mbox_msi_write);
960 if (rc) {
961 return dev_err_probe(dev, rc, "Failed to allocate %d MSIs\n",
962 mbox->msi_count);
963 }
964
965 for (i = 0; i < mbox->channel_count; i++) {
966 mchan = &mbox->channels[i];
967 if (mchan->msi_index == U32_MAX)
968 continue;
969 mchan->msi_irq = msi_get_virq(dev, mchan->msi_index);
970 }
971 }
972
973 /* Register mailbox controller */
974 rc = devm_mbox_controller_register(dev, &mbox->controller);
975 if (rc) {
976 dev_err_probe(dev, rc, "Registering SBI MPXY mailbox failed\n");
977 if (mbox->msi_count)
978 platform_device_msi_free_irqs_all(dev);
979 return rc;
980 }
981
982 #ifdef CONFIG_ACPI
983 struct acpi_device *adev = ACPI_COMPANION(dev);
984
985 if (adev)
986 acpi_dev_clear_dependencies(adev);
987 #endif
988
989 dev_info(dev, "mailbox registered with %d channels\n",
990 mbox->channel_count);
991 return 0;
992 }
993
mpxy_mbox_remove(struct platform_device * pdev)994 static void mpxy_mbox_remove(struct platform_device *pdev)
995 {
996 struct mpxy_mbox *mbox = platform_get_drvdata(pdev);
997
998 if (mbox->msi_count)
999 platform_device_msi_free_irqs_all(mbox->dev);
1000 }
1001
1002 static const struct of_device_id mpxy_mbox_of_match[] = {
1003 { .compatible = "riscv,sbi-mpxy-mbox" },
1004 {}
1005 };
1006 MODULE_DEVICE_TABLE(of, mpxy_mbox_of_match);
1007
1008 static const struct acpi_device_id mpxy_mbox_acpi_match[] = {
1009 { "RSCV0005" },
1010 {}
1011 };
1012 MODULE_DEVICE_TABLE(acpi, mpxy_mbox_acpi_match);
1013
1014 static struct platform_driver mpxy_mbox_driver = {
1015 .driver = {
1016 .name = "riscv-sbi-mpxy-mbox",
1017 .of_match_table = mpxy_mbox_of_match,
1018 .acpi_match_table = mpxy_mbox_acpi_match,
1019 },
1020 .probe = mpxy_mbox_probe,
1021 .remove = mpxy_mbox_remove,
1022 };
1023 module_platform_driver(mpxy_mbox_driver);
1024
1025 MODULE_LICENSE("GPL");
1026 MODULE_AUTHOR("Anup Patel <apatel@ventanamicro.com>");
1027 MODULE_DESCRIPTION("RISC-V SBI MPXY mailbox controller driver");
1028