vfio_platform_common.c 15 KB

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