vfio_platform_common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. * Copyright (C) 2013 - Virtual Open Systems
  3. * Author: Antonios Motakis <a.motakis@virtualopensystems.com>
  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, version 2, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/device.h>
  15. #include <linux/iommu.h>
  16. #include <linux/module.h>
  17. #include <linux/mutex.h>
  18. #include <linux/slab.h>
  19. #include <linux/types.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/vfio.h>
  22. #include "vfio_platform_private.h"
  23. static DEFINE_MUTEX(driver_lock);
  24. static int vfio_platform_regions_init(struct vfio_platform_device *vdev)
  25. {
  26. int cnt = 0, i;
  27. while (vdev->get_resource(vdev, cnt))
  28. cnt++;
  29. vdev->regions = kcalloc(cnt, sizeof(struct vfio_platform_region),
  30. GFP_KERNEL);
  31. if (!vdev->regions)
  32. return -ENOMEM;
  33. for (i = 0; i < cnt; i++) {
  34. struct resource *res =
  35. vdev->get_resource(vdev, i);
  36. if (!res)
  37. goto err;
  38. vdev->regions[i].addr = res->start;
  39. vdev->regions[i].size = resource_size(res);
  40. vdev->regions[i].flags = 0;
  41. switch (resource_type(res)) {
  42. case IORESOURCE_MEM:
  43. vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_MMIO;
  44. vdev->regions[i].flags |= VFIO_REGION_INFO_FLAG_READ;
  45. if (!(res->flags & IORESOURCE_READONLY))
  46. vdev->regions[i].flags |=
  47. VFIO_REGION_INFO_FLAG_WRITE;
  48. /*
  49. * Only regions addressed with PAGE granularity may be
  50. * MMAPed securely.
  51. */
  52. if (!(vdev->regions[i].addr & ~PAGE_MASK) &&
  53. !(vdev->regions[i].size & ~PAGE_MASK))
  54. vdev->regions[i].flags |=
  55. VFIO_REGION_INFO_FLAG_MMAP;
  56. break;
  57. case IORESOURCE_IO:
  58. vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_PIO;
  59. break;
  60. default:
  61. goto err;
  62. }
  63. }
  64. vdev->num_regions = cnt;
  65. return 0;
  66. err:
  67. kfree(vdev->regions);
  68. return -EINVAL;
  69. }
  70. static void vfio_platform_regions_cleanup(struct vfio_platform_device *vdev)
  71. {
  72. int i;
  73. for (i = 0; i < vdev->num_regions; i++)
  74. iounmap(vdev->regions[i].ioaddr);
  75. vdev->num_regions = 0;
  76. kfree(vdev->regions);
  77. }
  78. static void vfio_platform_release(void *device_data)
  79. {
  80. struct vfio_platform_device *vdev = device_data;
  81. mutex_lock(&driver_lock);
  82. if (!(--vdev->refcnt)) {
  83. vfio_platform_regions_cleanup(vdev);
  84. vfio_platform_irq_cleanup(vdev);
  85. }
  86. mutex_unlock(&driver_lock);
  87. module_put(THIS_MODULE);
  88. }
  89. static int vfio_platform_open(void *device_data)
  90. {
  91. struct vfio_platform_device *vdev = device_data;
  92. int ret;
  93. if (!try_module_get(THIS_MODULE))
  94. return -ENODEV;
  95. mutex_lock(&driver_lock);
  96. if (!vdev->refcnt) {
  97. ret = vfio_platform_regions_init(vdev);
  98. if (ret)
  99. goto err_reg;
  100. ret = vfio_platform_irq_init(vdev);
  101. if (ret)
  102. goto err_irq;
  103. }
  104. vdev->refcnt++;
  105. mutex_unlock(&driver_lock);
  106. return 0;
  107. err_irq:
  108. vfio_platform_regions_cleanup(vdev);
  109. err_reg:
  110. mutex_unlock(&driver_lock);
  111. module_put(THIS_MODULE);
  112. return ret;
  113. }
  114. static long vfio_platform_ioctl(void *device_data,
  115. unsigned int cmd, unsigned long arg)
  116. {
  117. struct vfio_platform_device *vdev = device_data;
  118. unsigned long minsz;
  119. if (cmd == VFIO_DEVICE_GET_INFO) {
  120. struct vfio_device_info info;
  121. minsz = offsetofend(struct vfio_device_info, num_irqs);
  122. if (copy_from_user(&info, (void __user *)arg, minsz))
  123. return -EFAULT;
  124. if (info.argsz < minsz)
  125. return -EINVAL;
  126. info.flags = vdev->flags;
  127. info.num_regions = vdev->num_regions;
  128. info.num_irqs = vdev->num_irqs;
  129. return copy_to_user((void __user *)arg, &info, minsz);
  130. } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
  131. struct vfio_region_info info;
  132. minsz = offsetofend(struct vfio_region_info, offset);
  133. if (copy_from_user(&info, (void __user *)arg, minsz))
  134. return -EFAULT;
  135. if (info.argsz < minsz)
  136. return -EINVAL;
  137. if (info.index >= vdev->num_regions)
  138. return -EINVAL;
  139. /* map offset to the physical address */
  140. info.offset = VFIO_PLATFORM_INDEX_TO_OFFSET(info.index);
  141. info.size = vdev->regions[info.index].size;
  142. info.flags = vdev->regions[info.index].flags;
  143. return copy_to_user((void __user *)arg, &info, minsz);
  144. } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
  145. struct vfio_irq_info info;
  146. minsz = offsetofend(struct vfio_irq_info, count);
  147. if (copy_from_user(&info, (void __user *)arg, minsz))
  148. return -EFAULT;
  149. if (info.argsz < minsz)
  150. return -EINVAL;
  151. if (info.index >= vdev->num_irqs)
  152. return -EINVAL;
  153. info.flags = vdev->irqs[info.index].flags;
  154. info.count = vdev->irqs[info.index].count;
  155. return copy_to_user((void __user *)arg, &info, minsz);
  156. } else if (cmd == VFIO_DEVICE_SET_IRQS) {
  157. struct vfio_irq_set hdr;
  158. u8 *data = NULL;
  159. int ret = 0;
  160. minsz = offsetofend(struct vfio_irq_set, count);
  161. if (copy_from_user(&hdr, (void __user *)arg, minsz))
  162. return -EFAULT;
  163. if (hdr.argsz < minsz)
  164. return -EINVAL;
  165. if (hdr.index >= vdev->num_irqs)
  166. return -EINVAL;
  167. if (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
  168. VFIO_IRQ_SET_ACTION_TYPE_MASK))
  169. return -EINVAL;
  170. if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
  171. size_t size;
  172. if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
  173. size = sizeof(uint8_t);
  174. else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
  175. size = sizeof(int32_t);
  176. else
  177. return -EINVAL;
  178. if (hdr.argsz - minsz < size)
  179. return -EINVAL;
  180. data = memdup_user((void __user *)(arg + minsz), size);
  181. if (IS_ERR(data))
  182. return PTR_ERR(data);
  183. }
  184. mutex_lock(&vdev->igate);
  185. ret = vfio_platform_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
  186. hdr.start, hdr.count, data);
  187. mutex_unlock(&vdev->igate);
  188. kfree(data);
  189. return ret;
  190. } else if (cmd == VFIO_DEVICE_RESET)
  191. return -EINVAL;
  192. return -ENOTTY;
  193. }
  194. static ssize_t vfio_platform_read_mmio(struct vfio_platform_region reg,
  195. char __user *buf, size_t count,
  196. loff_t off)
  197. {
  198. unsigned int done = 0;
  199. if (!reg.ioaddr) {
  200. reg.ioaddr =
  201. ioremap_nocache(reg.addr, reg.size);
  202. if (!reg.ioaddr)
  203. return -ENOMEM;
  204. }
  205. while (count) {
  206. size_t filled;
  207. if (count >= 4 && !(off % 4)) {
  208. u32 val;
  209. val = ioread32(reg.ioaddr + off);
  210. if (copy_to_user(buf, &val, 4))
  211. goto err;
  212. filled = 4;
  213. } else if (count >= 2 && !(off % 2)) {
  214. u16 val;
  215. val = ioread16(reg.ioaddr + off);
  216. if (copy_to_user(buf, &val, 2))
  217. goto err;
  218. filled = 2;
  219. } else {
  220. u8 val;
  221. val = ioread8(reg.ioaddr + off);
  222. if (copy_to_user(buf, &val, 1))
  223. goto err;
  224. filled = 1;
  225. }
  226. count -= filled;
  227. done += filled;
  228. off += filled;
  229. buf += filled;
  230. }
  231. return done;
  232. err:
  233. return -EFAULT;
  234. }
  235. static ssize_t vfio_platform_read(void *device_data, char __user *buf,
  236. size_t count, loff_t *ppos)
  237. {
  238. struct vfio_platform_device *vdev = device_data;
  239. unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
  240. loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
  241. if (index >= vdev->num_regions)
  242. return -EINVAL;
  243. if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ))
  244. return -EINVAL;
  245. if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
  246. return vfio_platform_read_mmio(vdev->regions[index],
  247. buf, count, off);
  248. else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
  249. return -EINVAL; /* not implemented */
  250. return -EINVAL;
  251. }
  252. static ssize_t vfio_platform_write_mmio(struct vfio_platform_region reg,
  253. const char __user *buf, size_t count,
  254. loff_t off)
  255. {
  256. unsigned int done = 0;
  257. if (!reg.ioaddr) {
  258. reg.ioaddr =
  259. ioremap_nocache(reg.addr, reg.size);
  260. if (!reg.ioaddr)
  261. return -ENOMEM;
  262. }
  263. while (count) {
  264. size_t filled;
  265. if (count >= 4 && !(off % 4)) {
  266. u32 val;
  267. if (copy_from_user(&val, buf, 4))
  268. goto err;
  269. iowrite32(val, reg.ioaddr + off);
  270. filled = 4;
  271. } else if (count >= 2 && !(off % 2)) {
  272. u16 val;
  273. if (copy_from_user(&val, buf, 2))
  274. goto err;
  275. iowrite16(val, reg.ioaddr + off);
  276. filled = 2;
  277. } else {
  278. u8 val;
  279. if (copy_from_user(&val, buf, 1))
  280. goto err;
  281. iowrite8(val, reg.ioaddr + off);
  282. filled = 1;
  283. }
  284. count -= filled;
  285. done += filled;
  286. off += filled;
  287. buf += filled;
  288. }
  289. return done;
  290. err:
  291. return -EFAULT;
  292. }
  293. static ssize_t vfio_platform_write(void *device_data, const char __user *buf,
  294. size_t count, loff_t *ppos)
  295. {
  296. struct vfio_platform_device *vdev = device_data;
  297. unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
  298. loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
  299. if (index >= vdev->num_regions)
  300. return -EINVAL;
  301. if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE))
  302. return -EINVAL;
  303. if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
  304. return vfio_platform_write_mmio(vdev->regions[index],
  305. buf, count, off);
  306. else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
  307. return -EINVAL; /* not implemented */
  308. return -EINVAL;
  309. }
  310. static int vfio_platform_mmap_mmio(struct vfio_platform_region region,
  311. struct vm_area_struct *vma)
  312. {
  313. u64 req_len, pgoff, req_start;
  314. req_len = vma->vm_end - vma->vm_start;
  315. pgoff = vma->vm_pgoff &
  316. ((1U << (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
  317. req_start = pgoff << PAGE_SHIFT;
  318. if (region.size < PAGE_SIZE || req_start + req_len > region.size)
  319. return -EINVAL;
  320. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  321. vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
  322. return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  323. req_len, vma->vm_page_prot);
  324. }
  325. static int vfio_platform_mmap(void *device_data, struct vm_area_struct *vma)
  326. {
  327. struct vfio_platform_device *vdev = device_data;
  328. unsigned int index;
  329. index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT);
  330. if (vma->vm_end < vma->vm_start)
  331. return -EINVAL;
  332. if (!(vma->vm_flags & VM_SHARED))
  333. return -EINVAL;
  334. if (index >= vdev->num_regions)
  335. return -EINVAL;
  336. if (vma->vm_start & ~PAGE_MASK)
  337. return -EINVAL;
  338. if (vma->vm_end & ~PAGE_MASK)
  339. return -EINVAL;
  340. if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP))
  341. return -EINVAL;
  342. if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ)
  343. && (vma->vm_flags & VM_READ))
  344. return -EINVAL;
  345. if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
  346. && (vma->vm_flags & VM_WRITE))
  347. return -EINVAL;
  348. vma->vm_private_data = vdev;
  349. if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
  350. return vfio_platform_mmap_mmio(vdev->regions[index], vma);
  351. else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
  352. return -EINVAL; /* not implemented */
  353. return -EINVAL;
  354. }
  355. static const struct vfio_device_ops vfio_platform_ops = {
  356. .name = "vfio-platform",
  357. .open = vfio_platform_open,
  358. .release = vfio_platform_release,
  359. .ioctl = vfio_platform_ioctl,
  360. .read = vfio_platform_read,
  361. .write = vfio_platform_write,
  362. .mmap = vfio_platform_mmap,
  363. };
  364. int vfio_platform_probe_common(struct vfio_platform_device *vdev,
  365. struct device *dev)
  366. {
  367. struct iommu_group *group;
  368. int ret;
  369. if (!vdev)
  370. return -EINVAL;
  371. group = iommu_group_get(dev);
  372. if (!group) {
  373. pr_err("VFIO: No IOMMU group for device %s\n", vdev->name);
  374. return -EINVAL;
  375. }
  376. ret = vfio_add_group_dev(dev, &vfio_platform_ops, vdev);
  377. if (ret) {
  378. iommu_group_put(group);
  379. return ret;
  380. }
  381. mutex_init(&vdev->igate);
  382. return 0;
  383. }
  384. EXPORT_SYMBOL_GPL(vfio_platform_probe_common);
  385. struct vfio_platform_device *vfio_platform_remove_common(struct device *dev)
  386. {
  387. struct vfio_platform_device *vdev;
  388. vdev = vfio_del_group_dev(dev);
  389. if (vdev)
  390. iommu_group_put(dev->iommu_group);
  391. return vdev;
  392. }
  393. EXPORT_SYMBOL_GPL(vfio_platform_remove_common);