vfio_platform_common.c 13 KB

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