GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / vfio / pci / vfio_pci_rdwr.c
1 /*
2  * VFIO PCI I/O Port & MMIO access
3  *
4  * Copyright (C) 2012 Red Hat, Inc.  All rights reserved.
5  *     Author: Alex Williamson <alex.williamson@redhat.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * Derived from original vfio:
12  * Copyright 2010 Cisco Systems, Inc.  All rights reserved.
13  * Author: Tom Lyon, pugs@cisco.com
14  */
15
16 #include <linux/fs.h>
17 #include <linux/pci.h>
18 #include <linux/uaccess.h>
19 #include <linux/io.h>
20 #include <linux/vfio.h>
21 #include <linux/vgaarb.h>
22
23 #include "vfio_pci_private.h"
24
25 #ifdef __LITTLE_ENDIAN
26 #define vfio_ioread64   ioread64
27 #define vfio_iowrite64  iowrite64
28 #define vfio_ioread32   ioread32
29 #define vfio_iowrite32  iowrite32
30 #define vfio_ioread16   ioread16
31 #define vfio_iowrite16  iowrite16
32 #else
33 #define vfio_ioread64   ioread64be
34 #define vfio_iowrite64  iowrite64be
35 #define vfio_ioread32   ioread32be
36 #define vfio_iowrite32  iowrite32be
37 #define vfio_ioread16   ioread16be
38 #define vfio_iowrite16  iowrite16be
39 #endif
40 #define vfio_ioread8    ioread8
41 #define vfio_iowrite8   iowrite8
42
43 /*
44  * Read or write from an __iomem region (MMIO or I/O port) with an excluded
45  * range which is inaccessible.  The excluded range drops writes and fills
46  * reads with -1.  This is intended for handling MSI-X vector tables and
47  * leftover space for ROM BARs.
48  */
49 static ssize_t do_io_rw(void __iomem *io, char __user *buf,
50                         loff_t off, size_t count, size_t x_start,
51                         size_t x_end, bool iswrite)
52 {
53         ssize_t done = 0;
54
55         while (count) {
56                 size_t fillable, filled;
57
58                 if (off < x_start)
59                         fillable = min(count, (size_t)(x_start - off));
60                 else if (off >= x_end)
61                         fillable = count;
62                 else
63                         fillable = 0;
64
65                 if (fillable >= 4 && !(off % 4)) {
66                         u32 val;
67
68                         if (iswrite) {
69                                 if (copy_from_user(&val, buf, 4))
70                                         return -EFAULT;
71
72                                 vfio_iowrite32(val, io + off);
73                         } else {
74                                 val = vfio_ioread32(io + off);
75
76                                 if (copy_to_user(buf, &val, 4))
77                                         return -EFAULT;
78                         }
79
80                         filled = 4;
81                 } else if (fillable >= 2 && !(off % 2)) {
82                         u16 val;
83
84                         if (iswrite) {
85                                 if (copy_from_user(&val, buf, 2))
86                                         return -EFAULT;
87
88                                 vfio_iowrite16(val, io + off);
89                         } else {
90                                 val = vfio_ioread16(io + off);
91
92                                 if (copy_to_user(buf, &val, 2))
93                                         return -EFAULT;
94                         }
95
96                         filled = 2;
97                 } else if (fillable) {
98                         u8 val;
99
100                         if (iswrite) {
101                                 if (copy_from_user(&val, buf, 1))
102                                         return -EFAULT;
103
104                                 vfio_iowrite8(val, io + off);
105                         } else {
106                                 val = vfio_ioread8(io + off);
107
108                                 if (copy_to_user(buf, &val, 1))
109                                         return -EFAULT;
110                         }
111
112                         filled = 1;
113                 } else {
114                         /* Fill reads with -1, drop writes */
115                         filled = min(count, (size_t)(x_end - off));
116                         if (!iswrite) {
117                                 u8 val = 0xFF;
118                                 size_t i;
119
120                                 for (i = 0; i < filled; i++)
121                                         if (copy_to_user(buf + i, &val, 1))
122                                                 return -EFAULT;
123                         }
124                 }
125
126                 count -= filled;
127                 done += filled;
128                 off += filled;
129                 buf += filled;
130         }
131
132         return done;
133 }
134
135 static int vfio_pci_setup_barmap(struct vfio_pci_device *vdev, int bar)
136 {
137         struct pci_dev *pdev = vdev->pdev;
138         int ret;
139         void __iomem *io;
140
141         if (vdev->barmap[bar])
142                 return 0;
143
144         ret = pci_request_selected_regions(pdev, 1 << bar, "vfio");
145         if (ret)
146                 return ret;
147
148         io = pci_iomap(pdev, bar, 0);
149         if (!io) {
150                 pci_release_selected_regions(pdev, 1 << bar);
151                 return -ENOMEM;
152         }
153
154         vdev->barmap[bar] = io;
155
156         return 0;
157 }
158
159 ssize_t vfio_pci_bar_rw(struct vfio_pci_device *vdev, char __user *buf,
160                         size_t count, loff_t *ppos, bool iswrite)
161 {
162         struct pci_dev *pdev = vdev->pdev;
163         loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
164         int bar = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
165         size_t x_start = 0, x_end = 0;
166         resource_size_t end;
167         void __iomem *io;
168         struct resource *res = &vdev->pdev->resource[bar];
169         ssize_t done;
170
171         if (pci_resource_start(pdev, bar))
172                 end = pci_resource_len(pdev, bar);
173         else if (bar == PCI_ROM_RESOURCE &&
174                  pdev->resource[bar].flags & IORESOURCE_ROM_SHADOW)
175                 end = 0x20000;
176         else
177                 return -EINVAL;
178
179         if (pos >= end)
180                 return -EINVAL;
181
182         count = min(count, (size_t)(end - pos));
183
184         if (res->flags & IORESOURCE_MEM) {
185                 down_read(&vdev->memory_lock);
186                 if (!__vfio_pci_memory_enabled(vdev)) {
187                         up_read(&vdev->memory_lock);
188                         return -EIO;
189                 }
190         }
191
192         if (bar == PCI_ROM_RESOURCE) {
193                 /*
194                  * The ROM can fill less space than the BAR, so we start the
195                  * excluded range at the end of the actual ROM.  This makes
196                  * filling large ROM BARs much faster.
197                  */
198                 io = pci_map_rom(pdev, &x_start);
199                 if (!io) {
200                         done = -ENOMEM;
201                         goto out;
202                 }
203                 x_end = end;
204         } else {
205                 int ret = vfio_pci_setup_barmap(vdev, bar);
206                 if (ret) {
207                         done = ret;
208                         goto out;
209                 }
210
211                 io = vdev->barmap[bar];
212         }
213
214         if (bar == vdev->msix_bar) {
215                 x_start = vdev->msix_offset;
216                 x_end = vdev->msix_offset + vdev->msix_size;
217         }
218
219         done = do_io_rw(io, buf, pos, count, x_start, x_end, iswrite);
220
221         if (done >= 0)
222                 *ppos += done;
223
224         if (bar == PCI_ROM_RESOURCE)
225                 pci_unmap_rom(pdev, io);
226 out:
227         if (res->flags & IORESOURCE_MEM)
228                 up_read(&vdev->memory_lock);
229
230         return done;
231 }
232
233 ssize_t vfio_pci_vga_rw(struct vfio_pci_device *vdev, char __user *buf,
234                                size_t count, loff_t *ppos, bool iswrite)
235 {
236         int ret;
237         loff_t off, pos = *ppos & VFIO_PCI_OFFSET_MASK;
238         void __iomem *iomem = NULL;
239         unsigned int rsrc;
240         bool is_ioport;
241         ssize_t done;
242
243         if (!vdev->has_vga)
244                 return -EINVAL;
245
246         if (pos > 0xbfffful)
247                 return -EINVAL;
248
249         switch ((u32)pos) {
250         case 0xa0000 ... 0xbffff:
251                 count = min(count, (size_t)(0xc0000 - pos));
252                 iomem = ioremap_nocache(0xa0000, 0xbffff - 0xa0000 + 1);
253                 off = pos - 0xa0000;
254                 rsrc = VGA_RSRC_LEGACY_MEM;
255                 is_ioport = false;
256                 break;
257         case 0x3b0 ... 0x3bb:
258                 count = min(count, (size_t)(0x3bc - pos));
259                 iomem = ioport_map(0x3b0, 0x3bb - 0x3b0 + 1);
260                 off = pos - 0x3b0;
261                 rsrc = VGA_RSRC_LEGACY_IO;
262                 is_ioport = true;
263                 break;
264         case 0x3c0 ... 0x3df:
265                 count = min(count, (size_t)(0x3e0 - pos));
266                 iomem = ioport_map(0x3c0, 0x3df - 0x3c0 + 1);
267                 off = pos - 0x3c0;
268                 rsrc = VGA_RSRC_LEGACY_IO;
269                 is_ioport = true;
270                 break;
271         default:
272                 return -EINVAL;
273         }
274
275         if (!iomem)
276                 return -ENOMEM;
277
278         ret = vga_get_interruptible(vdev->pdev, rsrc);
279         if (ret) {
280                 is_ioport ? ioport_unmap(iomem) : iounmap(iomem);
281                 return ret;
282         }
283
284         done = do_io_rw(iomem, buf, off, count, 0, 0, iswrite);
285
286         vga_put(vdev->pdev, rsrc);
287
288         is_ioport ? ioport_unmap(iomem) : iounmap(iomem);
289
290         if (done >= 0)
291                 *ppos += done;
292
293         return done;
294 }
295
296 static int vfio_pci_ioeventfd_handler(void *opaque, void *unused)
297 {
298         struct vfio_pci_ioeventfd *ioeventfd = opaque;
299
300         switch (ioeventfd->count) {
301         case 1:
302                 vfio_iowrite8(ioeventfd->data, ioeventfd->addr);
303                 break;
304         case 2:
305                 vfio_iowrite16(ioeventfd->data, ioeventfd->addr);
306                 break;
307         case 4:
308                 vfio_iowrite32(ioeventfd->data, ioeventfd->addr);
309                 break;
310 #ifdef iowrite64
311         case 8:
312                 vfio_iowrite64(ioeventfd->data, ioeventfd->addr);
313                 break;
314 #endif
315         }
316
317         return 0;
318 }
319
320 long vfio_pci_ioeventfd(struct vfio_pci_device *vdev, loff_t offset,
321                         uint64_t data, int count, int fd)
322 {
323         struct pci_dev *pdev = vdev->pdev;
324         loff_t pos = offset & VFIO_PCI_OFFSET_MASK;
325         int ret, bar = VFIO_PCI_OFFSET_TO_INDEX(offset);
326         struct vfio_pci_ioeventfd *ioeventfd;
327
328         /* Only support ioeventfds into BARs */
329         if (bar > VFIO_PCI_BAR5_REGION_INDEX)
330                 return -EINVAL;
331
332         if (pos + count > pci_resource_len(pdev, bar))
333                 return -EINVAL;
334
335         /* Disallow ioeventfds working around MSI-X table writes */
336         if (bar == vdev->msix_bar &&
337             !(pos + count <= vdev->msix_offset ||
338               pos >= vdev->msix_offset + vdev->msix_size))
339                 return -EINVAL;
340
341 #ifndef iowrite64
342         if (count == 8)
343                 return -EINVAL;
344 #endif
345
346         ret = vfio_pci_setup_barmap(vdev, bar);
347         if (ret)
348                 return ret;
349
350         mutex_lock(&vdev->ioeventfds_lock);
351
352         list_for_each_entry(ioeventfd, &vdev->ioeventfds_list, next) {
353                 if (ioeventfd->pos == pos && ioeventfd->bar == bar &&
354                     ioeventfd->data == data && ioeventfd->count == count) {
355                         if (fd == -1) {
356                                 vfio_virqfd_disable(&ioeventfd->virqfd);
357                                 list_del(&ioeventfd->next);
358                                 vdev->ioeventfds_nr--;
359                                 kfree(ioeventfd);
360                                 ret = 0;
361                         } else
362                                 ret = -EEXIST;
363
364                         goto out_unlock;
365                 }
366         }
367
368         if (fd < 0) {
369                 ret = -ENODEV;
370                 goto out_unlock;
371         }
372
373         if (vdev->ioeventfds_nr >= VFIO_PCI_IOEVENTFD_MAX) {
374                 ret = -ENOSPC;
375                 goto out_unlock;
376         }
377
378         ioeventfd = kzalloc(sizeof(*ioeventfd), GFP_KERNEL);
379         if (!ioeventfd) {
380                 ret = -ENOMEM;
381                 goto out_unlock;
382         }
383
384         ioeventfd->addr = vdev->barmap[bar] + pos;
385         ioeventfd->data = data;
386         ioeventfd->pos = pos;
387         ioeventfd->bar = bar;
388         ioeventfd->count = count;
389
390         ret = vfio_virqfd_enable(ioeventfd, vfio_pci_ioeventfd_handler,
391                                  NULL, NULL, &ioeventfd->virqfd, fd);
392         if (ret) {
393                 kfree(ioeventfd);
394                 goto out_unlock;
395         }
396
397         list_add(&ioeventfd->next, &vdev->ioeventfds_list);
398         vdev->ioeventfds_nr++;
399
400 out_unlock:
401         mutex_unlock(&vdev->ioeventfds_lock);
402
403         return ret;
404 }