vfio_platform_common.c 17 KB

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