vfio_platform_common.c 17 KB

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