vfio_platform_common.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. #define DRIVER_VERSION "0.10"
  24. #define DRIVER_AUTHOR "Antonios Motakis <a.motakis@virtualopensystems.com>"
  25. #define DRIVER_DESC "VFIO platform base module"
  26. static LIST_HEAD(reset_list);
  27. static DEFINE_MUTEX(driver_lock);
  28. static vfio_platform_reset_fn_t vfio_platform_lookup_reset(const char *compat,
  29. struct module **module)
  30. {
  31. struct vfio_platform_reset_node *iter;
  32. vfio_platform_reset_fn_t reset_fn = NULL;
  33. mutex_lock(&driver_lock);
  34. list_for_each_entry(iter, &reset_list, link) {
  35. if (!strcmp(iter->compat, compat) &&
  36. try_module_get(iter->owner)) {
  37. *module = iter->owner;
  38. reset_fn = iter->reset;
  39. break;
  40. }
  41. }
  42. mutex_unlock(&driver_lock);
  43. return reset_fn;
  44. }
  45. static void vfio_platform_get_reset(struct vfio_platform_device *vdev)
  46. {
  47. vdev->reset = vfio_platform_lookup_reset(vdev->compat,
  48. &vdev->reset_module);
  49. if (!vdev->reset) {
  50. request_module("vfio-reset:%s", vdev->compat);
  51. vdev->reset = vfio_platform_lookup_reset(vdev->compat,
  52. &vdev->reset_module);
  53. }
  54. }
  55. static void vfio_platform_put_reset(struct vfio_platform_device *vdev)
  56. {
  57. if (vdev->reset)
  58. module_put(vdev->reset_module);
  59. }
  60. static int vfio_platform_regions_init(struct vfio_platform_device *vdev)
  61. {
  62. int cnt = 0, i;
  63. while (vdev->get_resource(vdev, cnt))
  64. cnt++;
  65. vdev->regions = kcalloc(cnt, sizeof(struct vfio_platform_region),
  66. GFP_KERNEL);
  67. if (!vdev->regions)
  68. return -ENOMEM;
  69. for (i = 0; i < cnt; i++) {
  70. struct resource *res =
  71. vdev->get_resource(vdev, i);
  72. if (!res)
  73. goto err;
  74. vdev->regions[i].addr = res->start;
  75. vdev->regions[i].size = resource_size(res);
  76. vdev->regions[i].flags = 0;
  77. switch (resource_type(res)) {
  78. case IORESOURCE_MEM:
  79. vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_MMIO;
  80. vdev->regions[i].flags |= VFIO_REGION_INFO_FLAG_READ;
  81. if (!(res->flags & IORESOURCE_READONLY))
  82. vdev->regions[i].flags |=
  83. VFIO_REGION_INFO_FLAG_WRITE;
  84. /*
  85. * Only regions addressed with PAGE granularity may be
  86. * MMAPed securely.
  87. */
  88. if (!(vdev->regions[i].addr & ~PAGE_MASK) &&
  89. !(vdev->regions[i].size & ~PAGE_MASK))
  90. vdev->regions[i].flags |=
  91. VFIO_REGION_INFO_FLAG_MMAP;
  92. break;
  93. case IORESOURCE_IO:
  94. vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_PIO;
  95. break;
  96. default:
  97. goto err;
  98. }
  99. }
  100. vdev->num_regions = cnt;
  101. return 0;
  102. err:
  103. kfree(vdev->regions);
  104. return -EINVAL;
  105. }
  106. static void vfio_platform_regions_cleanup(struct vfio_platform_device *vdev)
  107. {
  108. int i;
  109. for (i = 0; i < vdev->num_regions; i++)
  110. iounmap(vdev->regions[i].ioaddr);
  111. vdev->num_regions = 0;
  112. kfree(vdev->regions);
  113. }
  114. static void vfio_platform_release(void *device_data)
  115. {
  116. struct vfio_platform_device *vdev = device_data;
  117. mutex_lock(&driver_lock);
  118. if (!(--vdev->refcnt)) {
  119. if (vdev->reset) {
  120. dev_info(vdev->device, "reset\n");
  121. vdev->reset(vdev);
  122. } else {
  123. dev_warn(vdev->device, "no reset function found!\n");
  124. }
  125. vfio_platform_regions_cleanup(vdev);
  126. vfio_platform_irq_cleanup(vdev);
  127. }
  128. mutex_unlock(&driver_lock);
  129. module_put(vdev->parent_module);
  130. }
  131. static int vfio_platform_open(void *device_data)
  132. {
  133. struct vfio_platform_device *vdev = device_data;
  134. int ret;
  135. if (!try_module_get(vdev->parent_module))
  136. return -ENODEV;
  137. mutex_lock(&driver_lock);
  138. if (!vdev->refcnt) {
  139. ret = vfio_platform_regions_init(vdev);
  140. if (ret)
  141. goto err_reg;
  142. ret = vfio_platform_irq_init(vdev);
  143. if (ret)
  144. goto err_irq;
  145. if (vdev->reset) {
  146. dev_info(vdev->device, "reset\n");
  147. vdev->reset(vdev);
  148. } else {
  149. dev_warn(vdev->device, "no reset function found!\n");
  150. }
  151. }
  152. vdev->refcnt++;
  153. mutex_unlock(&driver_lock);
  154. return 0;
  155. err_irq:
  156. vfio_platform_regions_cleanup(vdev);
  157. err_reg:
  158. mutex_unlock(&driver_lock);
  159. module_put(THIS_MODULE);
  160. return ret;
  161. }
  162. static long vfio_platform_ioctl(void *device_data,
  163. unsigned int cmd, unsigned long arg)
  164. {
  165. struct vfio_platform_device *vdev = device_data;
  166. unsigned long minsz;
  167. if (cmd == VFIO_DEVICE_GET_INFO) {
  168. struct vfio_device_info info;
  169. minsz = offsetofend(struct vfio_device_info, num_irqs);
  170. if (copy_from_user(&info, (void __user *)arg, minsz))
  171. return -EFAULT;
  172. if (info.argsz < minsz)
  173. return -EINVAL;
  174. if (vdev->reset)
  175. vdev->flags |= VFIO_DEVICE_FLAGS_RESET;
  176. info.flags = vdev->flags;
  177. info.num_regions = vdev->num_regions;
  178. info.num_irqs = vdev->num_irqs;
  179. return copy_to_user((void __user *)arg, &info, minsz) ?
  180. -EFAULT : 0;
  181. } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
  182. struct vfio_region_info info;
  183. minsz = offsetofend(struct vfio_region_info, offset);
  184. if (copy_from_user(&info, (void __user *)arg, minsz))
  185. return -EFAULT;
  186. if (info.argsz < minsz)
  187. return -EINVAL;
  188. if (info.index >= vdev->num_regions)
  189. return -EINVAL;
  190. /* map offset to the physical address */
  191. info.offset = VFIO_PLATFORM_INDEX_TO_OFFSET(info.index);
  192. info.size = vdev->regions[info.index].size;
  193. info.flags = vdev->regions[info.index].flags;
  194. return copy_to_user((void __user *)arg, &info, minsz) ?
  195. -EFAULT : 0;
  196. } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
  197. struct vfio_irq_info info;
  198. minsz = offsetofend(struct vfio_irq_info, count);
  199. if (copy_from_user(&info, (void __user *)arg, minsz))
  200. return -EFAULT;
  201. if (info.argsz < minsz)
  202. return -EINVAL;
  203. if (info.index >= vdev->num_irqs)
  204. return -EINVAL;
  205. info.flags = vdev->irqs[info.index].flags;
  206. info.count = vdev->irqs[info.index].count;
  207. return copy_to_user((void __user *)arg, &info, minsz) ?
  208. -EFAULT : 0;
  209. } else if (cmd == VFIO_DEVICE_SET_IRQS) {
  210. struct vfio_irq_set hdr;
  211. u8 *data = NULL;
  212. int ret = 0;
  213. minsz = offsetofend(struct vfio_irq_set, count);
  214. if (copy_from_user(&hdr, (void __user *)arg, minsz))
  215. return -EFAULT;
  216. if (hdr.argsz < minsz)
  217. return -EINVAL;
  218. if (hdr.index >= vdev->num_irqs)
  219. return -EINVAL;
  220. if (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
  221. VFIO_IRQ_SET_ACTION_TYPE_MASK))
  222. return -EINVAL;
  223. if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
  224. size_t size;
  225. if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
  226. size = sizeof(uint8_t);
  227. else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
  228. size = sizeof(int32_t);
  229. else
  230. return -EINVAL;
  231. if (hdr.argsz - minsz < size)
  232. return -EINVAL;
  233. data = memdup_user((void __user *)(arg + minsz), size);
  234. if (IS_ERR(data))
  235. return PTR_ERR(data);
  236. }
  237. mutex_lock(&vdev->igate);
  238. ret = vfio_platform_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
  239. hdr.start, hdr.count, data);
  240. mutex_unlock(&vdev->igate);
  241. kfree(data);
  242. return ret;
  243. } else if (cmd == VFIO_DEVICE_RESET) {
  244. if (vdev->reset)
  245. return vdev->reset(vdev);
  246. else
  247. return -EINVAL;
  248. }
  249. return -ENOTTY;
  250. }
  251. static ssize_t vfio_platform_read_mmio(struct vfio_platform_region *reg,
  252. char __user *buf, size_t count,
  253. loff_t off)
  254. {
  255. unsigned int done = 0;
  256. if (!reg->ioaddr) {
  257. reg->ioaddr =
  258. ioremap_nocache(reg->addr, reg->size);
  259. if (!reg->ioaddr)
  260. return -ENOMEM;
  261. }
  262. while (count) {
  263. size_t filled;
  264. if (count >= 4 && !(off % 4)) {
  265. u32 val;
  266. val = ioread32(reg->ioaddr + off);
  267. if (copy_to_user(buf, &val, 4))
  268. goto err;
  269. filled = 4;
  270. } else if (count >= 2 && !(off % 2)) {
  271. u16 val;
  272. val = ioread16(reg->ioaddr + off);
  273. if (copy_to_user(buf, &val, 2))
  274. goto err;
  275. filled = 2;
  276. } else {
  277. u8 val;
  278. val = ioread8(reg->ioaddr + off);
  279. if (copy_to_user(buf, &val, 1))
  280. goto err;
  281. filled = 1;
  282. }
  283. count -= filled;
  284. done += filled;
  285. off += filled;
  286. buf += filled;
  287. }
  288. return done;
  289. err:
  290. return -EFAULT;
  291. }
  292. static ssize_t vfio_platform_read(void *device_data, char __user *buf,
  293. size_t count, loff_t *ppos)
  294. {
  295. struct vfio_platform_device *vdev = device_data;
  296. unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
  297. loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
  298. if (index >= vdev->num_regions)
  299. return -EINVAL;
  300. if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ))
  301. return -EINVAL;
  302. if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
  303. return vfio_platform_read_mmio(&vdev->regions[index],
  304. buf, count, off);
  305. else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
  306. return -EINVAL; /* not implemented */
  307. return -EINVAL;
  308. }
  309. static ssize_t vfio_platform_write_mmio(struct vfio_platform_region *reg,
  310. const char __user *buf, size_t count,
  311. loff_t off)
  312. {
  313. unsigned int done = 0;
  314. if (!reg->ioaddr) {
  315. reg->ioaddr =
  316. ioremap_nocache(reg->addr, reg->size);
  317. if (!reg->ioaddr)
  318. return -ENOMEM;
  319. }
  320. while (count) {
  321. size_t filled;
  322. if (count >= 4 && !(off % 4)) {
  323. u32 val;
  324. if (copy_from_user(&val, buf, 4))
  325. goto err;
  326. iowrite32(val, reg->ioaddr + off);
  327. filled = 4;
  328. } else if (count >= 2 && !(off % 2)) {
  329. u16 val;
  330. if (copy_from_user(&val, buf, 2))
  331. goto err;
  332. iowrite16(val, reg->ioaddr + off);
  333. filled = 2;
  334. } else {
  335. u8 val;
  336. if (copy_from_user(&val, buf, 1))
  337. goto err;
  338. iowrite8(val, reg->ioaddr + off);
  339. filled = 1;
  340. }
  341. count -= filled;
  342. done += filled;
  343. off += filled;
  344. buf += filled;
  345. }
  346. return done;
  347. err:
  348. return -EFAULT;
  349. }
  350. static ssize_t vfio_platform_write(void *device_data, const char __user *buf,
  351. size_t count, loff_t *ppos)
  352. {
  353. struct vfio_platform_device *vdev = device_data;
  354. unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
  355. loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
  356. if (index >= vdev->num_regions)
  357. return -EINVAL;
  358. if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE))
  359. return -EINVAL;
  360. if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
  361. return vfio_platform_write_mmio(&vdev->regions[index],
  362. buf, count, off);
  363. else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
  364. return -EINVAL; /* not implemented */
  365. return -EINVAL;
  366. }
  367. static int vfio_platform_mmap_mmio(struct vfio_platform_region region,
  368. struct vm_area_struct *vma)
  369. {
  370. u64 req_len, pgoff, req_start;
  371. req_len = vma->vm_end - vma->vm_start;
  372. pgoff = vma->vm_pgoff &
  373. ((1U << (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
  374. req_start = pgoff << PAGE_SHIFT;
  375. if (region.size < PAGE_SIZE || req_start + req_len > region.size)
  376. return -EINVAL;
  377. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  378. vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
  379. return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  380. req_len, vma->vm_page_prot);
  381. }
  382. static int vfio_platform_mmap(void *device_data, struct vm_area_struct *vma)
  383. {
  384. struct vfio_platform_device *vdev = device_data;
  385. unsigned int index;
  386. index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT);
  387. if (vma->vm_end < vma->vm_start)
  388. return -EINVAL;
  389. if (!(vma->vm_flags & VM_SHARED))
  390. return -EINVAL;
  391. if (index >= vdev->num_regions)
  392. return -EINVAL;
  393. if (vma->vm_start & ~PAGE_MASK)
  394. return -EINVAL;
  395. if (vma->vm_end & ~PAGE_MASK)
  396. return -EINVAL;
  397. if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP))
  398. return -EINVAL;
  399. if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ)
  400. && (vma->vm_flags & VM_READ))
  401. return -EINVAL;
  402. if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
  403. && (vma->vm_flags & VM_WRITE))
  404. return -EINVAL;
  405. vma->vm_private_data = vdev;
  406. if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
  407. return vfio_platform_mmap_mmio(vdev->regions[index], vma);
  408. else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
  409. return -EINVAL; /* not implemented */
  410. return -EINVAL;
  411. }
  412. static const struct vfio_device_ops vfio_platform_ops = {
  413. .name = "vfio-platform",
  414. .open = vfio_platform_open,
  415. .release = vfio_platform_release,
  416. .ioctl = vfio_platform_ioctl,
  417. .read = vfio_platform_read,
  418. .write = vfio_platform_write,
  419. .mmap = vfio_platform_mmap,
  420. };
  421. int vfio_platform_probe_common(struct vfio_platform_device *vdev,
  422. struct device *dev)
  423. {
  424. struct iommu_group *group;
  425. int ret;
  426. if (!vdev)
  427. return -EINVAL;
  428. ret = device_property_read_string(dev, "compatible", &vdev->compat);
  429. if (ret) {
  430. pr_err("VFIO: cannot retrieve compat for %s\n", vdev->name);
  431. return -EINVAL;
  432. }
  433. vdev->device = dev;
  434. group = iommu_group_get(dev);
  435. if (!group) {
  436. pr_err("VFIO: No IOMMU group for device %s\n", vdev->name);
  437. return -EINVAL;
  438. }
  439. ret = vfio_add_group_dev(dev, &vfio_platform_ops, vdev);
  440. if (ret) {
  441. iommu_group_put(group);
  442. return ret;
  443. }
  444. vfio_platform_get_reset(vdev);
  445. mutex_init(&vdev->igate);
  446. return 0;
  447. }
  448. EXPORT_SYMBOL_GPL(vfio_platform_probe_common);
  449. struct vfio_platform_device *vfio_platform_remove_common(struct device *dev)
  450. {
  451. struct vfio_platform_device *vdev;
  452. vdev = vfio_del_group_dev(dev);
  453. if (vdev) {
  454. vfio_platform_put_reset(vdev);
  455. iommu_group_put(dev->iommu_group);
  456. }
  457. return vdev;
  458. }
  459. EXPORT_SYMBOL_GPL(vfio_platform_remove_common);
  460. void __vfio_platform_register_reset(struct vfio_platform_reset_node *node)
  461. {
  462. mutex_lock(&driver_lock);
  463. list_add(&node->link, &reset_list);
  464. mutex_unlock(&driver_lock);
  465. }
  466. EXPORT_SYMBOL_GPL(__vfio_platform_register_reset);
  467. void vfio_platform_unregister_reset(const char *compat,
  468. vfio_platform_reset_fn_t fn)
  469. {
  470. struct vfio_platform_reset_node *iter, *temp;
  471. mutex_lock(&driver_lock);
  472. list_for_each_entry_safe(iter, temp, &reset_list, link) {
  473. if (!strcmp(iter->compat, compat) && (iter->reset == fn)) {
  474. list_del(&iter->link);
  475. break;
  476. }
  477. }
  478. mutex_unlock(&driver_lock);
  479. }
  480. EXPORT_SYMBOL_GPL(vfio_platform_unregister_reset);
  481. MODULE_VERSION(DRIVER_VERSION);
  482. MODULE_LICENSE("GPL v2");
  483. MODULE_AUTHOR(DRIVER_AUTHOR);
  484. MODULE_DESCRIPTION(DRIVER_DESC);