vfio_platform_common.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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_pm;
  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. err_pm:
  238. vfio_platform_irq_cleanup(vdev);
  239. err_irq:
  240. vfio_platform_regions_cleanup(vdev);
  241. err_reg:
  242. mutex_unlock(&driver_lock);
  243. module_put(THIS_MODULE);
  244. return ret;
  245. }
  246. static long vfio_platform_ioctl(void *device_data,
  247. unsigned int cmd, unsigned long arg)
  248. {
  249. struct vfio_platform_device *vdev = device_data;
  250. unsigned long minsz;
  251. if (cmd == VFIO_DEVICE_GET_INFO) {
  252. struct vfio_device_info info;
  253. minsz = offsetofend(struct vfio_device_info, num_irqs);
  254. if (copy_from_user(&info, (void __user *)arg, minsz))
  255. return -EFAULT;
  256. if (info.argsz < minsz)
  257. return -EINVAL;
  258. if (vfio_platform_has_reset(vdev))
  259. vdev->flags |= VFIO_DEVICE_FLAGS_RESET;
  260. info.flags = vdev->flags;
  261. info.num_regions = vdev->num_regions;
  262. info.num_irqs = vdev->num_irqs;
  263. return copy_to_user((void __user *)arg, &info, minsz) ?
  264. -EFAULT : 0;
  265. } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
  266. struct vfio_region_info info;
  267. minsz = offsetofend(struct vfio_region_info, offset);
  268. if (copy_from_user(&info, (void __user *)arg, minsz))
  269. return -EFAULT;
  270. if (info.argsz < minsz)
  271. return -EINVAL;
  272. if (info.index >= vdev->num_regions)
  273. return -EINVAL;
  274. /* map offset to the physical address */
  275. info.offset = VFIO_PLATFORM_INDEX_TO_OFFSET(info.index);
  276. info.size = vdev->regions[info.index].size;
  277. info.flags = vdev->regions[info.index].flags;
  278. return copy_to_user((void __user *)arg, &info, minsz) ?
  279. -EFAULT : 0;
  280. } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
  281. struct vfio_irq_info info;
  282. minsz = offsetofend(struct vfio_irq_info, count);
  283. if (copy_from_user(&info, (void __user *)arg, minsz))
  284. return -EFAULT;
  285. if (info.argsz < minsz)
  286. return -EINVAL;
  287. if (info.index >= vdev->num_irqs)
  288. return -EINVAL;
  289. info.flags = vdev->irqs[info.index].flags;
  290. info.count = vdev->irqs[info.index].count;
  291. return copy_to_user((void __user *)arg, &info, minsz) ?
  292. -EFAULT : 0;
  293. } else if (cmd == VFIO_DEVICE_SET_IRQS) {
  294. struct vfio_irq_set hdr;
  295. u8 *data = NULL;
  296. int ret = 0;
  297. size_t data_size = 0;
  298. minsz = offsetofend(struct vfio_irq_set, count);
  299. if (copy_from_user(&hdr, (void __user *)arg, minsz))
  300. return -EFAULT;
  301. ret = vfio_set_irqs_validate_and_prepare(&hdr, vdev->num_irqs,
  302. vdev->num_irqs, &data_size);
  303. if (ret)
  304. return ret;
  305. if (data_size) {
  306. data = memdup_user((void __user *)(arg + minsz),
  307. data_size);
  308. if (IS_ERR(data))
  309. return PTR_ERR(data);
  310. }
  311. mutex_lock(&vdev->igate);
  312. ret = vfio_platform_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
  313. hdr.start, hdr.count, data);
  314. mutex_unlock(&vdev->igate);
  315. kfree(data);
  316. return ret;
  317. } else if (cmd == VFIO_DEVICE_RESET) {
  318. return vfio_platform_call_reset(vdev, NULL);
  319. }
  320. return -ENOTTY;
  321. }
  322. static ssize_t vfio_platform_read_mmio(struct vfio_platform_region *reg,
  323. char __user *buf, size_t count,
  324. loff_t off)
  325. {
  326. unsigned int done = 0;
  327. if (!reg->ioaddr) {
  328. reg->ioaddr =
  329. ioremap_nocache(reg->addr, reg->size);
  330. if (!reg->ioaddr)
  331. return -ENOMEM;
  332. }
  333. while (count) {
  334. size_t filled;
  335. if (count >= 4 && !(off % 4)) {
  336. u32 val;
  337. val = ioread32(reg->ioaddr + off);
  338. if (copy_to_user(buf, &val, 4))
  339. goto err;
  340. filled = 4;
  341. } else if (count >= 2 && !(off % 2)) {
  342. u16 val;
  343. val = ioread16(reg->ioaddr + off);
  344. if (copy_to_user(buf, &val, 2))
  345. goto err;
  346. filled = 2;
  347. } else {
  348. u8 val;
  349. val = ioread8(reg->ioaddr + off);
  350. if (copy_to_user(buf, &val, 1))
  351. goto err;
  352. filled = 1;
  353. }
  354. count -= filled;
  355. done += filled;
  356. off += filled;
  357. buf += filled;
  358. }
  359. return done;
  360. err:
  361. return -EFAULT;
  362. }
  363. static ssize_t vfio_platform_read(void *device_data, char __user *buf,
  364. size_t count, loff_t *ppos)
  365. {
  366. struct vfio_platform_device *vdev = device_data;
  367. unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
  368. loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
  369. if (index >= vdev->num_regions)
  370. return -EINVAL;
  371. if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ))
  372. return -EINVAL;
  373. if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
  374. return vfio_platform_read_mmio(&vdev->regions[index],
  375. buf, count, off);
  376. else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
  377. return -EINVAL; /* not implemented */
  378. return -EINVAL;
  379. }
  380. static ssize_t vfio_platform_write_mmio(struct vfio_platform_region *reg,
  381. const char __user *buf, size_t count,
  382. loff_t off)
  383. {
  384. unsigned int done = 0;
  385. if (!reg->ioaddr) {
  386. reg->ioaddr =
  387. ioremap_nocache(reg->addr, reg->size);
  388. if (!reg->ioaddr)
  389. return -ENOMEM;
  390. }
  391. while (count) {
  392. size_t filled;
  393. if (count >= 4 && !(off % 4)) {
  394. u32 val;
  395. if (copy_from_user(&val, buf, 4))
  396. goto err;
  397. iowrite32(val, reg->ioaddr + off);
  398. filled = 4;
  399. } else if (count >= 2 && !(off % 2)) {
  400. u16 val;
  401. if (copy_from_user(&val, buf, 2))
  402. goto err;
  403. iowrite16(val, reg->ioaddr + off);
  404. filled = 2;
  405. } else {
  406. u8 val;
  407. if (copy_from_user(&val, buf, 1))
  408. goto err;
  409. iowrite8(val, reg->ioaddr + off);
  410. filled = 1;
  411. }
  412. count -= filled;
  413. done += filled;
  414. off += filled;
  415. buf += filled;
  416. }
  417. return done;
  418. err:
  419. return -EFAULT;
  420. }
  421. static ssize_t vfio_platform_write(void *device_data, const char __user *buf,
  422. size_t count, loff_t *ppos)
  423. {
  424. struct vfio_platform_device *vdev = device_data;
  425. unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
  426. loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
  427. if (index >= vdev->num_regions)
  428. return -EINVAL;
  429. if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE))
  430. return -EINVAL;
  431. if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
  432. return vfio_platform_write_mmio(&vdev->regions[index],
  433. buf, count, off);
  434. else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
  435. return -EINVAL; /* not implemented */
  436. return -EINVAL;
  437. }
  438. static int vfio_platform_mmap_mmio(struct vfio_platform_region region,
  439. struct vm_area_struct *vma)
  440. {
  441. u64 req_len, pgoff, req_start;
  442. req_len = vma->vm_end - vma->vm_start;
  443. pgoff = vma->vm_pgoff &
  444. ((1U << (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
  445. req_start = pgoff << PAGE_SHIFT;
  446. if (region.size < PAGE_SIZE || req_start + req_len > region.size)
  447. return -EINVAL;
  448. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  449. vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
  450. return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  451. req_len, vma->vm_page_prot);
  452. }
  453. static int vfio_platform_mmap(void *device_data, struct vm_area_struct *vma)
  454. {
  455. struct vfio_platform_device *vdev = device_data;
  456. unsigned int index;
  457. index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT);
  458. if (vma->vm_end < vma->vm_start)
  459. return -EINVAL;
  460. if (!(vma->vm_flags & VM_SHARED))
  461. return -EINVAL;
  462. if (index >= vdev->num_regions)
  463. return -EINVAL;
  464. if (vma->vm_start & ~PAGE_MASK)
  465. return -EINVAL;
  466. if (vma->vm_end & ~PAGE_MASK)
  467. return -EINVAL;
  468. if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP))
  469. return -EINVAL;
  470. if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ)
  471. && (vma->vm_flags & VM_READ))
  472. return -EINVAL;
  473. if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
  474. && (vma->vm_flags & VM_WRITE))
  475. return -EINVAL;
  476. vma->vm_private_data = vdev;
  477. if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
  478. return vfio_platform_mmap_mmio(vdev->regions[index], vma);
  479. else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
  480. return -EINVAL; /* not implemented */
  481. return -EINVAL;
  482. }
  483. static const struct vfio_device_ops vfio_platform_ops = {
  484. .name = "vfio-platform",
  485. .open = vfio_platform_open,
  486. .release = vfio_platform_release,
  487. .ioctl = vfio_platform_ioctl,
  488. .read = vfio_platform_read,
  489. .write = vfio_platform_write,
  490. .mmap = vfio_platform_mmap,
  491. };
  492. static int vfio_platform_of_probe(struct vfio_platform_device *vdev,
  493. struct device *dev)
  494. {
  495. int ret;
  496. ret = device_property_read_string(dev, "compatible",
  497. &vdev->compat);
  498. if (ret)
  499. pr_err("VFIO: Cannot retrieve compat for %s\n", vdev->name);
  500. return ret;
  501. }
  502. /*
  503. * There can be two kernel build combinations. One build where
  504. * ACPI is not selected in Kconfig and another one with the ACPI Kconfig.
  505. *
  506. * In the first case, vfio_platform_acpi_probe will return since
  507. * acpi_disabled is 1. DT user will not see any kind of messages from
  508. * ACPI.
  509. *
  510. * In the second case, both DT and ACPI is compiled in but the system is
  511. * booting with any of these combinations.
  512. *
  513. * If the firmware is DT type, then acpi_disabled is 1. The ACPI probe routine
  514. * terminates immediately without any messages.
  515. *
  516. * If the firmware is ACPI type, then acpi_disabled is 0. All other checks are
  517. * valid checks. We cannot claim that this system is DT.
  518. */
  519. int vfio_platform_probe_common(struct vfio_platform_device *vdev,
  520. struct device *dev)
  521. {
  522. struct iommu_group *group;
  523. int ret;
  524. if (!vdev)
  525. return -EINVAL;
  526. ret = vfio_platform_acpi_probe(vdev, dev);
  527. if (ret)
  528. ret = vfio_platform_of_probe(vdev, dev);
  529. if (ret)
  530. return ret;
  531. vdev->device = dev;
  532. ret = vfio_platform_get_reset(vdev);
  533. if (ret && vdev->reset_required) {
  534. pr_err("VFIO: No reset function found for device %s\n",
  535. vdev->name);
  536. return ret;
  537. }
  538. group = vfio_iommu_group_get(dev);
  539. if (!group) {
  540. pr_err("VFIO: No IOMMU group for device %s\n", vdev->name);
  541. ret = -EINVAL;
  542. goto put_reset;
  543. }
  544. ret = vfio_add_group_dev(dev, &vfio_platform_ops, vdev);
  545. if (ret)
  546. goto put_iommu;
  547. mutex_init(&vdev->igate);
  548. pm_runtime_enable(vdev->device);
  549. return 0;
  550. put_iommu:
  551. vfio_iommu_group_put(group, dev);
  552. put_reset:
  553. vfio_platform_put_reset(vdev);
  554. return ret;
  555. }
  556. EXPORT_SYMBOL_GPL(vfio_platform_probe_common);
  557. struct vfio_platform_device *vfio_platform_remove_common(struct device *dev)
  558. {
  559. struct vfio_platform_device *vdev;
  560. vdev = vfio_del_group_dev(dev);
  561. if (vdev) {
  562. pm_runtime_disable(vdev->device);
  563. vfio_platform_put_reset(vdev);
  564. vfio_iommu_group_put(dev->iommu_group, dev);
  565. }
  566. return vdev;
  567. }
  568. EXPORT_SYMBOL_GPL(vfio_platform_remove_common);
  569. void __vfio_platform_register_reset(struct vfio_platform_reset_node *node)
  570. {
  571. mutex_lock(&driver_lock);
  572. list_add(&node->link, &reset_list);
  573. mutex_unlock(&driver_lock);
  574. }
  575. EXPORT_SYMBOL_GPL(__vfio_platform_register_reset);
  576. void vfio_platform_unregister_reset(const char *compat,
  577. vfio_platform_reset_fn_t fn)
  578. {
  579. struct vfio_platform_reset_node *iter, *temp;
  580. mutex_lock(&driver_lock);
  581. list_for_each_entry_safe(iter, temp, &reset_list, link) {
  582. if (!strcmp(iter->compat, compat) && (iter->of_reset == fn)) {
  583. list_del(&iter->link);
  584. break;
  585. }
  586. }
  587. mutex_unlock(&driver_lock);
  588. }
  589. EXPORT_SYMBOL_GPL(vfio_platform_unregister_reset);
  590. MODULE_VERSION(DRIVER_VERSION);
  591. MODULE_LICENSE("GPL v2");
  592. MODULE_AUTHOR(DRIVER_AUTHOR);
  593. MODULE_DESCRIPTION(DRIVER_DESC);