vfio_platform_common.c 17 KB

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