GNU Linux-libre 6.1.90-gnu
[releases.git] / arch / um / drivers / virt-pci.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2020 Intel Corporation
4  * Author: Johannes Berg <johannes@sipsolutions.net>
5  */
6 #include <linux/module.h>
7 #include <linux/pci.h>
8 #include <linux/virtio.h>
9 #include <linux/virtio_config.h>
10 #include <linux/logic_iomem.h>
11 #include <linux/irqdomain.h>
12 #include <linux/virtio_pcidev.h>
13 #include <linux/virtio-uml.h>
14 #include <linux/delay.h>
15 #include <linux/msi.h>
16 #include <asm/unaligned.h>
17 #include <irq_kern.h>
18
19 #define MAX_DEVICES 8
20 #define MAX_MSI_VECTORS 32
21 #define CFG_SPACE_SIZE 4096
22
23 /* for MSI-X we have a 32-bit payload */
24 #define MAX_IRQ_MSG_SIZE (sizeof(struct virtio_pcidev_msg) + sizeof(u32))
25 #define NUM_IRQ_MSGS    10
26
27 #define HANDLE_NO_FREE(ptr) ((void *)((unsigned long)(ptr) | 1))
28 #define HANDLE_IS_NO_FREE(ptr) ((unsigned long)(ptr) & 1)
29
30 struct um_pci_device {
31         struct virtio_device *vdev;
32
33         /* for now just standard BARs */
34         u8 resptr[PCI_STD_NUM_BARS];
35
36         struct virtqueue *cmd_vq, *irq_vq;
37
38 #define UM_PCI_STAT_WAITING     0
39         unsigned long status;
40
41         int irq;
42 };
43
44 struct um_pci_device_reg {
45         struct um_pci_device *dev;
46         void __iomem *iomem;
47 };
48
49 static struct pci_host_bridge *bridge;
50 static DEFINE_MUTEX(um_pci_mtx);
51 static struct um_pci_device_reg um_pci_devices[MAX_DEVICES];
52 static struct fwnode_handle *um_pci_fwnode;
53 static struct irq_domain *um_pci_inner_domain;
54 static struct irq_domain *um_pci_msi_domain;
55 static unsigned long um_pci_msi_used[BITS_TO_LONGS(MAX_MSI_VECTORS)];
56
57 #define UM_VIRT_PCI_MAXDELAY 40000
58
59 struct um_pci_message_buffer {
60         struct virtio_pcidev_msg hdr;
61         u8 data[8];
62 };
63
64 static struct um_pci_message_buffer __percpu *um_pci_msg_bufs;
65
66 static int um_pci_send_cmd(struct um_pci_device *dev,
67                            struct virtio_pcidev_msg *cmd,
68                            unsigned int cmd_size,
69                            const void *extra, unsigned int extra_size,
70                            void *out, unsigned int out_size)
71 {
72         struct scatterlist out_sg, extra_sg, in_sg;
73         struct scatterlist *sgs_list[] = {
74                 [0] = &out_sg,
75                 [1] = extra ? &extra_sg : &in_sg,
76                 [2] = extra ? &in_sg : NULL,
77         };
78         struct um_pci_message_buffer *buf;
79         int delay_count = 0;
80         int ret, len;
81         bool posted;
82
83         if (WARN_ON(cmd_size < sizeof(*cmd) || cmd_size > sizeof(*buf)))
84                 return -EINVAL;
85
86         switch (cmd->op) {
87         case VIRTIO_PCIDEV_OP_CFG_WRITE:
88         case VIRTIO_PCIDEV_OP_MMIO_WRITE:
89         case VIRTIO_PCIDEV_OP_MMIO_MEMSET:
90                 /* in PCI, writes are posted, so don't wait */
91                 posted = !out;
92                 WARN_ON(!posted);
93                 break;
94         default:
95                 posted = false;
96                 break;
97         }
98
99         buf = get_cpu_var(um_pci_msg_bufs);
100         if (buf)
101                 memcpy(buf, cmd, cmd_size);
102
103         if (posted) {
104                 u8 *ncmd = kmalloc(cmd_size + extra_size, GFP_ATOMIC);
105
106                 if (ncmd) {
107                         memcpy(ncmd, cmd, cmd_size);
108                         if (extra)
109                                 memcpy(ncmd + cmd_size, extra, extra_size);
110                         cmd = (void *)ncmd;
111                         cmd_size += extra_size;
112                         extra = NULL;
113                         extra_size = 0;
114                 } else {
115                         /* try without allocating memory */
116                         posted = false;
117                         cmd = (void *)buf;
118                 }
119         } else {
120                 cmd = (void *)buf;
121         }
122
123         sg_init_one(&out_sg, cmd, cmd_size);
124         if (extra)
125                 sg_init_one(&extra_sg, extra, extra_size);
126         if (out)
127                 sg_init_one(&in_sg, out, out_size);
128
129         /* add to internal virtio queue */
130         ret = virtqueue_add_sgs(dev->cmd_vq, sgs_list,
131                                 extra ? 2 : 1,
132                                 out ? 1 : 0,
133                                 posted ? cmd : HANDLE_NO_FREE(cmd),
134                                 GFP_ATOMIC);
135         if (ret) {
136                 if (posted)
137                         kfree(cmd);
138                 goto out;
139         }
140
141         if (posted) {
142                 virtqueue_kick(dev->cmd_vq);
143                 ret = 0;
144                 goto out;
145         }
146
147         /* kick and poll for getting a response on the queue */
148         set_bit(UM_PCI_STAT_WAITING, &dev->status);
149         virtqueue_kick(dev->cmd_vq);
150
151         while (1) {
152                 void *completed = virtqueue_get_buf(dev->cmd_vq, &len);
153
154                 if (completed == HANDLE_NO_FREE(cmd))
155                         break;
156
157                 if (completed && !HANDLE_IS_NO_FREE(completed))
158                         kfree(completed);
159
160                 if (WARN_ONCE(virtqueue_is_broken(dev->cmd_vq) ||
161                               ++delay_count > UM_VIRT_PCI_MAXDELAY,
162                               "um virt-pci delay: %d", delay_count)) {
163                         ret = -EIO;
164                         break;
165                 }
166                 udelay(1);
167         }
168         clear_bit(UM_PCI_STAT_WAITING, &dev->status);
169
170 out:
171         put_cpu_var(um_pci_msg_bufs);
172         return ret;
173 }
174
175 static unsigned long um_pci_cfgspace_read(void *priv, unsigned int offset,
176                                           int size)
177 {
178         struct um_pci_device_reg *reg = priv;
179         struct um_pci_device *dev = reg->dev;
180         struct virtio_pcidev_msg hdr = {
181                 .op = VIRTIO_PCIDEV_OP_CFG_READ,
182                 .size = size,
183                 .addr = offset,
184         };
185         /* buf->data is maximum size - we may only use parts of it */
186         struct um_pci_message_buffer *buf;
187         u8 *data;
188         unsigned long ret = ULONG_MAX;
189         size_t bytes = sizeof(buf->data);
190
191         if (!dev)
192                 return ULONG_MAX;
193
194         buf = get_cpu_var(um_pci_msg_bufs);
195         data = buf->data;
196
197         if (buf)
198                 memset(data, 0xff, bytes);
199
200         switch (size) {
201         case 1:
202         case 2:
203         case 4:
204 #ifdef CONFIG_64BIT
205         case 8:
206 #endif
207                 break;
208         default:
209                 WARN(1, "invalid config space read size %d\n", size);
210                 goto out;
211         }
212
213         if (um_pci_send_cmd(dev, &hdr, sizeof(hdr), NULL, 0, data, bytes))
214                 goto out;
215
216         switch (size) {
217         case 1:
218                 ret = data[0];
219                 break;
220         case 2:
221                 ret = le16_to_cpup((void *)data);
222                 break;
223         case 4:
224                 ret = le32_to_cpup((void *)data);
225                 break;
226 #ifdef CONFIG_64BIT
227         case 8:
228                 ret = le64_to_cpup((void *)data);
229                 break;
230 #endif
231         default:
232                 break;
233         }
234
235 out:
236         put_cpu_var(um_pci_msg_bufs);
237         return ret;
238 }
239
240 static void um_pci_cfgspace_write(void *priv, unsigned int offset, int size,
241                                   unsigned long val)
242 {
243         struct um_pci_device_reg *reg = priv;
244         struct um_pci_device *dev = reg->dev;
245         struct {
246                 struct virtio_pcidev_msg hdr;
247                 /* maximum size - we may only use parts of it */
248                 u8 data[8];
249         } msg = {
250                 .hdr = {
251                         .op = VIRTIO_PCIDEV_OP_CFG_WRITE,
252                         .size = size,
253                         .addr = offset,
254                 },
255         };
256
257         if (!dev)
258                 return;
259
260         switch (size) {
261         case 1:
262                 msg.data[0] = (u8)val;
263                 break;
264         case 2:
265                 put_unaligned_le16(val, (void *)msg.data);
266                 break;
267         case 4:
268                 put_unaligned_le32(val, (void *)msg.data);
269                 break;
270 #ifdef CONFIG_64BIT
271         case 8:
272                 put_unaligned_le64(val, (void *)msg.data);
273                 break;
274 #endif
275         default:
276                 WARN(1, "invalid config space write size %d\n", size);
277                 return;
278         }
279
280         WARN_ON(um_pci_send_cmd(dev, &msg.hdr, sizeof(msg), NULL, 0, NULL, 0));
281 }
282
283 static const struct logic_iomem_ops um_pci_device_cfgspace_ops = {
284         .read = um_pci_cfgspace_read,
285         .write = um_pci_cfgspace_write,
286 };
287
288 static void um_pci_bar_copy_from(void *priv, void *buffer,
289                                  unsigned int offset, int size)
290 {
291         u8 *resptr = priv;
292         struct um_pci_device *dev = container_of(resptr - *resptr,
293                                                  struct um_pci_device,
294                                                  resptr[0]);
295         struct virtio_pcidev_msg hdr = {
296                 .op = VIRTIO_PCIDEV_OP_MMIO_READ,
297                 .bar = *resptr,
298                 .size = size,
299                 .addr = offset,
300         };
301
302         memset(buffer, 0xff, size);
303
304         um_pci_send_cmd(dev, &hdr, sizeof(hdr), NULL, 0, buffer, size);
305 }
306
307 static unsigned long um_pci_bar_read(void *priv, unsigned int offset,
308                                      int size)
309 {
310         /* buf->data is maximum size - we may only use parts of it */
311         struct um_pci_message_buffer *buf;
312         u8 *data;
313         unsigned long ret = ULONG_MAX;
314
315         buf = get_cpu_var(um_pci_msg_bufs);
316         data = buf->data;
317
318         switch (size) {
319         case 1:
320         case 2:
321         case 4:
322 #ifdef CONFIG_64BIT
323         case 8:
324 #endif
325                 break;
326         default:
327                 WARN(1, "invalid config space read size %d\n", size);
328                 goto out;
329         }
330
331         um_pci_bar_copy_from(priv, data, offset, size);
332
333         switch (size) {
334         case 1:
335                 ret = data[0];
336                 break;
337         case 2:
338                 ret = le16_to_cpup((void *)data);
339                 break;
340         case 4:
341                 ret = le32_to_cpup((void *)data);
342                 break;
343 #ifdef CONFIG_64BIT
344         case 8:
345                 ret = le64_to_cpup((void *)data);
346                 break;
347 #endif
348         default:
349                 break;
350         }
351
352 out:
353         put_cpu_var(um_pci_msg_bufs);
354         return ret;
355 }
356
357 static void um_pci_bar_copy_to(void *priv, unsigned int offset,
358                                const void *buffer, int size)
359 {
360         u8 *resptr = priv;
361         struct um_pci_device *dev = container_of(resptr - *resptr,
362                                                  struct um_pci_device,
363                                                  resptr[0]);
364         struct virtio_pcidev_msg hdr = {
365                 .op = VIRTIO_PCIDEV_OP_MMIO_WRITE,
366                 .bar = *resptr,
367                 .size = size,
368                 .addr = offset,
369         };
370
371         um_pci_send_cmd(dev, &hdr, sizeof(hdr), buffer, size, NULL, 0);
372 }
373
374 static void um_pci_bar_write(void *priv, unsigned int offset, int size,
375                              unsigned long val)
376 {
377         /* maximum size - we may only use parts of it */
378         u8 data[8];
379
380         switch (size) {
381         case 1:
382                 data[0] = (u8)val;
383                 break;
384         case 2:
385                 put_unaligned_le16(val, (void *)data);
386                 break;
387         case 4:
388                 put_unaligned_le32(val, (void *)data);
389                 break;
390 #ifdef CONFIG_64BIT
391         case 8:
392                 put_unaligned_le64(val, (void *)data);
393                 break;
394 #endif
395         default:
396                 WARN(1, "invalid config space write size %d\n", size);
397                 return;
398         }
399
400         um_pci_bar_copy_to(priv, offset, data, size);
401 }
402
403 static void um_pci_bar_set(void *priv, unsigned int offset, u8 value, int size)
404 {
405         u8 *resptr = priv;
406         struct um_pci_device *dev = container_of(resptr - *resptr,
407                                                  struct um_pci_device,
408                                                  resptr[0]);
409         struct {
410                 struct virtio_pcidev_msg hdr;
411                 u8 data;
412         } msg = {
413                 .hdr = {
414                         .op = VIRTIO_PCIDEV_OP_CFG_WRITE,
415                         .bar = *resptr,
416                         .size = size,
417                         .addr = offset,
418                 },
419                 .data = value,
420         };
421
422         um_pci_send_cmd(dev, &msg.hdr, sizeof(msg), NULL, 0, NULL, 0);
423 }
424
425 static const struct logic_iomem_ops um_pci_device_bar_ops = {
426         .read = um_pci_bar_read,
427         .write = um_pci_bar_write,
428         .set = um_pci_bar_set,
429         .copy_from = um_pci_bar_copy_from,
430         .copy_to = um_pci_bar_copy_to,
431 };
432
433 static void __iomem *um_pci_map_bus(struct pci_bus *bus, unsigned int devfn,
434                                     int where)
435 {
436         struct um_pci_device_reg *dev;
437         unsigned int busn = bus->number;
438
439         if (busn > 0)
440                 return NULL;
441
442         /* not allowing functions for now ... */
443         if (devfn % 8)
444                 return NULL;
445
446         if (devfn / 8 >= ARRAY_SIZE(um_pci_devices))
447                 return NULL;
448
449         dev = &um_pci_devices[devfn / 8];
450         if (!dev)
451                 return NULL;
452
453         return (void __iomem *)((unsigned long)dev->iomem + where);
454 }
455
456 static struct pci_ops um_pci_ops = {
457         .map_bus = um_pci_map_bus,
458         .read = pci_generic_config_read,
459         .write = pci_generic_config_write,
460 };
461
462 static void um_pci_rescan(void)
463 {
464         pci_lock_rescan_remove();
465         pci_rescan_bus(bridge->bus);
466         pci_unlock_rescan_remove();
467 }
468
469 static void um_pci_irq_vq_addbuf(struct virtqueue *vq, void *buf, bool kick)
470 {
471         struct scatterlist sg[1];
472
473         sg_init_one(sg, buf, MAX_IRQ_MSG_SIZE);
474         if (virtqueue_add_inbuf(vq, sg, 1, buf, GFP_ATOMIC))
475                 kfree(buf);
476         else if (kick)
477                 virtqueue_kick(vq);
478 }
479
480 static void um_pci_handle_irq_message(struct virtqueue *vq,
481                                       struct virtio_pcidev_msg *msg)
482 {
483         struct virtio_device *vdev = vq->vdev;
484         struct um_pci_device *dev = vdev->priv;
485
486         /* we should properly chain interrupts, but on ARCH=um we don't care */
487
488         switch (msg->op) {
489         case VIRTIO_PCIDEV_OP_INT:
490                 generic_handle_irq(dev->irq);
491                 break;
492         case VIRTIO_PCIDEV_OP_MSI:
493                 /* our MSI message is just the interrupt number */
494                 if (msg->size == sizeof(u32))
495                         generic_handle_irq(le32_to_cpup((void *)msg->data));
496                 else
497                         generic_handle_irq(le16_to_cpup((void *)msg->data));
498                 break;
499         case VIRTIO_PCIDEV_OP_PME:
500                 /* nothing to do - we already woke up due to the message */
501                 break;
502         default:
503                 dev_err(&vdev->dev, "unexpected virt-pci message %d\n", msg->op);
504                 break;
505         }
506 }
507
508 static void um_pci_cmd_vq_cb(struct virtqueue *vq)
509 {
510         struct virtio_device *vdev = vq->vdev;
511         struct um_pci_device *dev = vdev->priv;
512         void *cmd;
513         int len;
514
515         if (test_bit(UM_PCI_STAT_WAITING, &dev->status))
516                 return;
517
518         while ((cmd = virtqueue_get_buf(vq, &len))) {
519                 if (WARN_ON(HANDLE_IS_NO_FREE(cmd)))
520                         continue;
521                 kfree(cmd);
522         }
523 }
524
525 static void um_pci_irq_vq_cb(struct virtqueue *vq)
526 {
527         struct virtio_pcidev_msg *msg;
528         int len;
529
530         while ((msg = virtqueue_get_buf(vq, &len))) {
531                 if (len >= sizeof(*msg))
532                         um_pci_handle_irq_message(vq, msg);
533
534                 /* recycle the message buffer */
535                 um_pci_irq_vq_addbuf(vq, msg, true);
536         }
537 }
538
539 static int um_pci_init_vqs(struct um_pci_device *dev)
540 {
541         struct virtqueue *vqs[2];
542         static const char *const names[2] = { "cmd", "irq" };
543         vq_callback_t *cbs[2] = { um_pci_cmd_vq_cb, um_pci_irq_vq_cb };
544         int err, i;
545
546         err = virtio_find_vqs(dev->vdev, 2, vqs, cbs, names, NULL);
547         if (err)
548                 return err;
549
550         dev->cmd_vq = vqs[0];
551         dev->irq_vq = vqs[1];
552
553         virtio_device_ready(dev->vdev);
554
555         for (i = 0; i < NUM_IRQ_MSGS; i++) {
556                 void *msg = kzalloc(MAX_IRQ_MSG_SIZE, GFP_KERNEL);
557
558                 if (msg)
559                         um_pci_irq_vq_addbuf(dev->irq_vq, msg, false);
560         }
561
562         virtqueue_kick(dev->irq_vq);
563
564         return 0;
565 }
566
567 static int um_pci_virtio_probe(struct virtio_device *vdev)
568 {
569         struct um_pci_device *dev;
570         int i, free = -1;
571         int err = -ENOSPC;
572
573         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
574         if (!dev)
575                 return -ENOMEM;
576
577         dev->vdev = vdev;
578         vdev->priv = dev;
579
580         mutex_lock(&um_pci_mtx);
581         for (i = 0; i < MAX_DEVICES; i++) {
582                 if (um_pci_devices[i].dev)
583                         continue;
584                 free = i;
585                 break;
586         }
587
588         if (free < 0)
589                 goto error;
590
591         err = um_pci_init_vqs(dev);
592         if (err)
593                 goto error;
594
595         dev->irq = irq_alloc_desc(numa_node_id());
596         if (dev->irq < 0) {
597                 err = dev->irq;
598                 goto err_reset;
599         }
600         um_pci_devices[free].dev = dev;
601         vdev->priv = dev;
602
603         mutex_unlock(&um_pci_mtx);
604
605         device_set_wakeup_enable(&vdev->dev, true);
606
607         /*
608          * In order to do suspend-resume properly, don't allow VQs
609          * to be suspended.
610          */
611         virtio_uml_set_no_vq_suspend(vdev, true);
612
613         um_pci_rescan();
614         return 0;
615 err_reset:
616         virtio_reset_device(vdev);
617         vdev->config->del_vqs(vdev);
618 error:
619         mutex_unlock(&um_pci_mtx);
620         kfree(dev);
621         return err;
622 }
623
624 static void um_pci_virtio_remove(struct virtio_device *vdev)
625 {
626         struct um_pci_device *dev = vdev->priv;
627         int i;
628
629         device_set_wakeup_enable(&vdev->dev, false);
630
631         mutex_lock(&um_pci_mtx);
632         for (i = 0; i < MAX_DEVICES; i++) {
633                 if (um_pci_devices[i].dev != dev)
634                         continue;
635
636                 um_pci_devices[i].dev = NULL;
637                 irq_free_desc(dev->irq);
638
639                 break;
640         }
641         mutex_unlock(&um_pci_mtx);
642
643         if (i < MAX_DEVICES) {
644                 struct pci_dev *pci_dev;
645
646                 pci_dev = pci_get_slot(bridge->bus, i);
647                 if (pci_dev)
648                         pci_stop_and_remove_bus_device_locked(pci_dev);
649         }
650
651         /* Stop all virtqueues */
652         virtio_reset_device(vdev);
653         dev->cmd_vq = NULL;
654         dev->irq_vq = NULL;
655         vdev->config->del_vqs(vdev);
656
657         kfree(dev);
658 }
659
660 static struct virtio_device_id id_table[] = {
661         { CONFIG_UML_PCI_OVER_VIRTIO_DEVICE_ID, VIRTIO_DEV_ANY_ID },
662         { 0 },
663 };
664 MODULE_DEVICE_TABLE(virtio, id_table);
665
666 static struct virtio_driver um_pci_virtio_driver = {
667         .driver.name = "virtio-pci",
668         .driver.owner = THIS_MODULE,
669         .id_table = id_table,
670         .probe = um_pci_virtio_probe,
671         .remove = um_pci_virtio_remove,
672 };
673
674 static struct resource virt_cfgspace_resource = {
675         .name = "PCI config space",
676         .start = 0xf0000000 - MAX_DEVICES * CFG_SPACE_SIZE,
677         .end = 0xf0000000 - 1,
678         .flags = IORESOURCE_MEM,
679 };
680
681 static long um_pci_map_cfgspace(unsigned long offset, size_t size,
682                                 const struct logic_iomem_ops **ops,
683                                 void **priv)
684 {
685         if (WARN_ON(size > CFG_SPACE_SIZE || offset % CFG_SPACE_SIZE))
686                 return -EINVAL;
687
688         if (offset / CFG_SPACE_SIZE < MAX_DEVICES) {
689                 *ops = &um_pci_device_cfgspace_ops;
690                 *priv = &um_pci_devices[offset / CFG_SPACE_SIZE];
691                 return 0;
692         }
693
694         WARN(1, "cannot map offset 0x%lx/0x%zx\n", offset, size);
695         return -ENOENT;
696 }
697
698 static const struct logic_iomem_region_ops um_pci_cfgspace_ops = {
699         .map = um_pci_map_cfgspace,
700 };
701
702 static struct resource virt_iomem_resource = {
703         .name = "PCI iomem",
704         .start = 0xf0000000,
705         .end = 0xffffffff,
706         .flags = IORESOURCE_MEM,
707 };
708
709 struct um_pci_map_iomem_data {
710         unsigned long offset;
711         size_t size;
712         const struct logic_iomem_ops **ops;
713         void **priv;
714         long ret;
715 };
716
717 static int um_pci_map_iomem_walk(struct pci_dev *pdev, void *_data)
718 {
719         struct um_pci_map_iomem_data *data = _data;
720         struct um_pci_device_reg *reg = &um_pci_devices[pdev->devfn / 8];
721         struct um_pci_device *dev;
722         int i;
723
724         if (!reg->dev)
725                 return 0;
726
727         for (i = 0; i < ARRAY_SIZE(dev->resptr); i++) {
728                 struct resource *r = &pdev->resource[i];
729
730                 if ((r->flags & IORESOURCE_TYPE_BITS) != IORESOURCE_MEM)
731                         continue;
732
733                 /*
734                  * must be the whole or part of the resource,
735                  * not allowed to only overlap
736                  */
737                 if (data->offset < r->start || data->offset > r->end)
738                         continue;
739                 if (data->offset + data->size - 1 > r->end)
740                         continue;
741
742                 dev = reg->dev;
743                 *data->ops = &um_pci_device_bar_ops;
744                 dev->resptr[i] = i;
745                 *data->priv = &dev->resptr[i];
746                 data->ret = data->offset - r->start;
747
748                 /* no need to continue */
749                 return 1;
750         }
751
752         return 0;
753 }
754
755 static long um_pci_map_iomem(unsigned long offset, size_t size,
756                              const struct logic_iomem_ops **ops,
757                              void **priv)
758 {
759         struct um_pci_map_iomem_data data = {
760                 /* we want the full address here */
761                 .offset = offset + virt_iomem_resource.start,
762                 .size = size,
763                 .ops = ops,
764                 .priv = priv,
765                 .ret = -ENOENT,
766         };
767
768         pci_walk_bus(bridge->bus, um_pci_map_iomem_walk, &data);
769         return data.ret;
770 }
771
772 static const struct logic_iomem_region_ops um_pci_iomem_ops = {
773         .map = um_pci_map_iomem,
774 };
775
776 static void um_pci_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
777 {
778         /*
779          * This is a very low address and not actually valid 'physical' memory
780          * in UML, so we can simply map MSI(-X) vectors to there, it cannot be
781          * legitimately written to by the device in any other way.
782          * We use the (virtual) IRQ number here as the message to simplify the
783          * code that receives the message, where for now we simply trust the
784          * device to send the correct message.
785          */
786         msg->address_hi = 0;
787         msg->address_lo = 0xa0000;
788         msg->data = data->irq;
789 }
790
791 static struct irq_chip um_pci_msi_bottom_irq_chip = {
792         .name = "UM virtio MSI",
793         .irq_compose_msi_msg = um_pci_compose_msi_msg,
794 };
795
796 static int um_pci_inner_domain_alloc(struct irq_domain *domain,
797                                      unsigned int virq, unsigned int nr_irqs,
798                                      void *args)
799 {
800         unsigned long bit;
801
802         WARN_ON(nr_irqs != 1);
803
804         mutex_lock(&um_pci_mtx);
805         bit = find_first_zero_bit(um_pci_msi_used, MAX_MSI_VECTORS);
806         if (bit >= MAX_MSI_VECTORS) {
807                 mutex_unlock(&um_pci_mtx);
808                 return -ENOSPC;
809         }
810
811         set_bit(bit, um_pci_msi_used);
812         mutex_unlock(&um_pci_mtx);
813
814         irq_domain_set_info(domain, virq, bit, &um_pci_msi_bottom_irq_chip,
815                             domain->host_data, handle_simple_irq,
816                             NULL, NULL);
817
818         return 0;
819 }
820
821 static void um_pci_inner_domain_free(struct irq_domain *domain,
822                                      unsigned int virq, unsigned int nr_irqs)
823 {
824         struct irq_data *d = irq_domain_get_irq_data(domain, virq);
825
826         mutex_lock(&um_pci_mtx);
827
828         if (!test_bit(d->hwirq, um_pci_msi_used))
829                 pr_err("trying to free unused MSI#%lu\n", d->hwirq);
830         else
831                 __clear_bit(d->hwirq, um_pci_msi_used);
832
833         mutex_unlock(&um_pci_mtx);
834 }
835
836 static const struct irq_domain_ops um_pci_inner_domain_ops = {
837         .alloc = um_pci_inner_domain_alloc,
838         .free = um_pci_inner_domain_free,
839 };
840
841 static struct irq_chip um_pci_msi_irq_chip = {
842         .name = "UM virtio PCIe MSI",
843         .irq_mask = pci_msi_mask_irq,
844         .irq_unmask = pci_msi_unmask_irq,
845 };
846
847 static struct msi_domain_info um_pci_msi_domain_info = {
848         .flags  = MSI_FLAG_USE_DEF_DOM_OPS |
849                   MSI_FLAG_USE_DEF_CHIP_OPS |
850                   MSI_FLAG_PCI_MSIX,
851         .chip   = &um_pci_msi_irq_chip,
852 };
853
854 static struct resource busn_resource = {
855         .name   = "PCI busn",
856         .start  = 0,
857         .end    = 0,
858         .flags  = IORESOURCE_BUS,
859 };
860
861 static int um_pci_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
862 {
863         struct um_pci_device_reg *reg = &um_pci_devices[pdev->devfn / 8];
864
865         if (WARN_ON(!reg->dev))
866                 return -EINVAL;
867
868         /* Yes, we map all pins to the same IRQ ... doesn't matter for now. */
869         return reg->dev->irq;
870 }
871
872 void *pci_root_bus_fwnode(struct pci_bus *bus)
873 {
874         return um_pci_fwnode;
875 }
876
877 static int __init um_pci_init(void)
878 {
879         int err, i;
880
881         WARN_ON(logic_iomem_add_region(&virt_cfgspace_resource,
882                                        &um_pci_cfgspace_ops));
883         WARN_ON(logic_iomem_add_region(&virt_iomem_resource,
884                                        &um_pci_iomem_ops));
885
886         if (WARN(CONFIG_UML_PCI_OVER_VIRTIO_DEVICE_ID < 0,
887                  "No virtio device ID configured for PCI - no PCI support\n"))
888                 return 0;
889
890         um_pci_msg_bufs = alloc_percpu(struct um_pci_message_buffer);
891         if (!um_pci_msg_bufs)
892                 return -ENOMEM;
893
894         bridge = pci_alloc_host_bridge(0);
895         if (!bridge) {
896                 err = -ENOMEM;
897                 goto free;
898         }
899
900         um_pci_fwnode = irq_domain_alloc_named_fwnode("um-pci");
901         if (!um_pci_fwnode) {
902                 err = -ENOMEM;
903                 goto free;
904         }
905
906         um_pci_inner_domain = __irq_domain_add(um_pci_fwnode, MAX_MSI_VECTORS,
907                                                MAX_MSI_VECTORS, 0,
908                                                &um_pci_inner_domain_ops, NULL);
909         if (!um_pci_inner_domain) {
910                 err = -ENOMEM;
911                 goto free;
912         }
913
914         um_pci_msi_domain = pci_msi_create_irq_domain(um_pci_fwnode,
915                                                       &um_pci_msi_domain_info,
916                                                       um_pci_inner_domain);
917         if (!um_pci_msi_domain) {
918                 err = -ENOMEM;
919                 goto free;
920         }
921
922         pci_add_resource(&bridge->windows, &virt_iomem_resource);
923         pci_add_resource(&bridge->windows, &busn_resource);
924         bridge->ops = &um_pci_ops;
925         bridge->map_irq = um_pci_map_irq;
926
927         for (i = 0; i < MAX_DEVICES; i++) {
928                 resource_size_t start;
929
930                 start = virt_cfgspace_resource.start + i * CFG_SPACE_SIZE;
931                 um_pci_devices[i].iomem = ioremap(start, CFG_SPACE_SIZE);
932                 if (WARN(!um_pci_devices[i].iomem, "failed to map %d\n", i)) {
933                         err = -ENOMEM;
934                         goto free;
935                 }
936         }
937
938         err = pci_host_probe(bridge);
939         if (err)
940                 goto free;
941
942         err = register_virtio_driver(&um_pci_virtio_driver);
943         if (err)
944                 goto free;
945         return 0;
946 free:
947         if (um_pci_inner_domain)
948                 irq_domain_remove(um_pci_inner_domain);
949         if (um_pci_fwnode)
950                 irq_domain_free_fwnode(um_pci_fwnode);
951         if (bridge) {
952                 pci_free_resource_list(&bridge->windows);
953                 pci_free_host_bridge(bridge);
954         }
955         free_percpu(um_pci_msg_bufs);
956         return err;
957 }
958 module_init(um_pci_init);
959
960 static void __exit um_pci_exit(void)
961 {
962         unregister_virtio_driver(&um_pci_virtio_driver);
963         irq_domain_remove(um_pci_msi_domain);
964         irq_domain_remove(um_pci_inner_domain);
965         pci_free_resource_list(&bridge->windows);
966         pci_free_host_bridge(bridge);
967         free_percpu(um_pci_msg_bufs);
968 }
969 module_exit(um_pci_exit);