2
0

818-vfio-support-layerscape.patch 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. From e6af99cc1d56322fc960d072af1a7e0e9285b90c Mon Sep 17 00:00:00 2001
  2. From: Yangbo Lu <[email protected]>
  3. Date: Thu, 5 Jul 2018 17:39:43 +0800
  4. Subject: [PATCH 30/32] vfio: support layerscape
  5. This is an integrated patch for layerscape vfio support.
  6. Signed-off-by: Bharat Bhushan <[email protected]>
  7. Signed-off-by: Eric Auger <[email protected]>
  8. Signed-off-by: Robin Murphy <[email protected]>
  9. Signed-off-by: Wei Yongjun <[email protected]>
  10. Signed-off-by: Yangbo Lu <[email protected]>
  11. ---
  12. drivers/vfio/Kconfig | 1 +
  13. drivers/vfio/Makefile | 1 +
  14. drivers/vfio/fsl-mc/Kconfig | 9 +
  15. drivers/vfio/fsl-mc/Makefile | 2 +
  16. drivers/vfio/fsl-mc/vfio_fsl_mc.c | 752 ++++++++++++++++++++++
  17. drivers/vfio/fsl-mc/vfio_fsl_mc_intr.c | 199 ++++++
  18. drivers/vfio/fsl-mc/vfio_fsl_mc_private.h | 55 ++
  19. drivers/vfio/vfio_iommu_type1.c | 39 +-
  20. include/uapi/linux/vfio.h | 1 +
  21. 9 files changed, 1057 insertions(+), 2 deletions(-)
  22. create mode 100644 drivers/vfio/fsl-mc/Kconfig
  23. create mode 100644 drivers/vfio/fsl-mc/Makefile
  24. create mode 100644 drivers/vfio/fsl-mc/vfio_fsl_mc.c
  25. create mode 100644 drivers/vfio/fsl-mc/vfio_fsl_mc_intr.c
  26. create mode 100644 drivers/vfio/fsl-mc/vfio_fsl_mc_private.h
  27. --- a/drivers/vfio/Kconfig
  28. +++ b/drivers/vfio/Kconfig
  29. @@ -48,4 +48,5 @@ menuconfig VFIO_NOIOMMU
  30. source "drivers/vfio/pci/Kconfig"
  31. source "drivers/vfio/platform/Kconfig"
  32. +source "drivers/vfio/fsl-mc/Kconfig"
  33. source "virt/lib/Kconfig"
  34. --- a/drivers/vfio/Makefile
  35. +++ b/drivers/vfio/Makefile
  36. @@ -7,3 +7,4 @@ obj-$(CONFIG_VFIO_IOMMU_SPAPR_TCE) += vf
  37. obj-$(CONFIG_VFIO_SPAPR_EEH) += vfio_spapr_eeh.o
  38. obj-$(CONFIG_VFIO_PCI) += pci/
  39. obj-$(CONFIG_VFIO_PLATFORM) += platform/
  40. +obj-$(CONFIG_VFIO_FSL_MC) += fsl-mc/
  41. --- /dev/null
  42. +++ b/drivers/vfio/fsl-mc/Kconfig
  43. @@ -0,0 +1,9 @@
  44. +config VFIO_FSL_MC
  45. + tristate "VFIO support for QorIQ DPAA2 fsl-mc bus devices"
  46. + depends on VFIO && FSL_MC_BUS && EVENTFD
  47. + help
  48. + Driver to enable support for the VFIO QorIQ DPAA2 fsl-mc
  49. + (Management Complex) devices. This is required to passthrough
  50. + fsl-mc bus devices using the VFIO framework.
  51. +
  52. + If you don't know what to do here, say N.
  53. --- /dev/null
  54. +++ b/drivers/vfio/fsl-mc/Makefile
  55. @@ -0,0 +1,2 @@
  56. +vfio-fsl_mc-y := vfio_fsl_mc.o
  57. +obj-$(CONFIG_VFIO_FSL_MC) += vfio_fsl_mc.o vfio_fsl_mc_intr.o
  58. --- /dev/null
  59. +++ b/drivers/vfio/fsl-mc/vfio_fsl_mc.c
  60. @@ -0,0 +1,752 @@
  61. +/*
  62. + * Freescale Management Complex (MC) device passthrough using VFIO
  63. + *
  64. + * Copyright (C) 2013-2016 Freescale Semiconductor, Inc.
  65. + * Copyright 2016-2017 NXP
  66. + * Author: Bharat Bhushan <[email protected]>
  67. + *
  68. + * This file is licensed under the terms of the GNU General Public
  69. + * License version 2. This program is licensed "as is" without any
  70. + * warranty of any kind, whether express or implied.
  71. + */
  72. +
  73. +#include <linux/device.h>
  74. +#include <linux/iommu.h>
  75. +#include <linux/module.h>
  76. +#include <linux/mutex.h>
  77. +#include <linux/slab.h>
  78. +#include <linux/types.h>
  79. +#include <linux/vfio.h>
  80. +#include <linux/delay.h>
  81. +
  82. +#include <linux/fsl/mc.h>
  83. +
  84. +#include "vfio_fsl_mc_private.h"
  85. +
  86. +#define DRIVER_VERSION "0.10"
  87. +#define DRIVER_AUTHOR "Bharat Bhushan <[email protected]>"
  88. +#define DRIVER_DESC "VFIO for FSL-MC devices - User Level meta-driver"
  89. +
  90. +static DEFINE_MUTEX(driver_lock);
  91. +
  92. +/* FSl-MC device regions (address and size) are aligned to 64K.
  93. + * While MC firmware reports size less than 64K for some objects (it actually
  94. + * reports size which does not include reserved space beyond valid bytes).
  95. + * Align the size to PAGE_SIZE for userspace to mmap.
  96. + */
  97. +static size_t aligned_region_size(struct fsl_mc_device *mc_dev, int index)
  98. +{
  99. + size_t size;
  100. +
  101. + size = resource_size(&mc_dev->regions[index]);
  102. + return PAGE_ALIGN(size);
  103. +}
  104. +
  105. +static int vfio_fsl_mc_regions_init(struct vfio_fsl_mc_device *vdev)
  106. +{
  107. + struct fsl_mc_device *mc_dev = vdev->mc_dev;
  108. + int count = mc_dev->obj_desc.region_count;
  109. + int i;
  110. +
  111. + vdev->regions = kcalloc(count, sizeof(struct vfio_fsl_mc_region),
  112. + GFP_KERNEL);
  113. + if (!vdev->regions)
  114. + return -ENOMEM;
  115. +
  116. + for (i = 0; i < mc_dev->obj_desc.region_count; i++) {
  117. + vdev->regions[i].addr = mc_dev->regions[i].start;
  118. + vdev->regions[i].size = aligned_region_size(mc_dev, i);
  119. + vdev->regions[i].type = VFIO_FSL_MC_REGION_TYPE_MMIO;
  120. + if (mc_dev->regions[i].flags & IORESOURCE_CACHEABLE)
  121. + vdev->regions[i].type |=
  122. + VFIO_FSL_MC_REGION_TYPE_CACHEABLE;
  123. + vdev->regions[i].flags = VFIO_REGION_INFO_FLAG_MMAP;
  124. + vdev->regions[i].flags |= VFIO_REGION_INFO_FLAG_READ;
  125. + if (!(mc_dev->regions[i].flags & IORESOURCE_READONLY))
  126. + vdev->regions[i].flags |= VFIO_REGION_INFO_FLAG_WRITE;
  127. + }
  128. +
  129. + vdev->num_regions = mc_dev->obj_desc.region_count;
  130. + return 0;
  131. +}
  132. +
  133. +static void vfio_fsl_mc_regions_cleanup(struct vfio_fsl_mc_device *vdev)
  134. +{
  135. + int i;
  136. +
  137. + for (i = 0; i < vdev->num_regions; i++)
  138. + iounmap(vdev->regions[i].ioaddr);
  139. +
  140. + vdev->num_regions = 0;
  141. + kfree(vdev->regions);
  142. +}
  143. +
  144. +static int vfio_fsl_mc_open(void *device_data)
  145. +{
  146. + struct vfio_fsl_mc_device *vdev = device_data;
  147. + int ret;
  148. +
  149. + if (!try_module_get(THIS_MODULE))
  150. + return -ENODEV;
  151. +
  152. + mutex_lock(&driver_lock);
  153. + if (!vdev->refcnt) {
  154. + ret = vfio_fsl_mc_regions_init(vdev);
  155. + if (ret)
  156. + goto error_region_init;
  157. +
  158. + ret = vfio_fsl_mc_irqs_init(vdev);
  159. + if (ret)
  160. + goto error_irq_init;
  161. + }
  162. +
  163. + vdev->refcnt++;
  164. + mutex_unlock(&driver_lock);
  165. + return 0;
  166. +
  167. +error_irq_init:
  168. + vfio_fsl_mc_regions_cleanup(vdev);
  169. +error_region_init:
  170. + mutex_unlock(&driver_lock);
  171. + if (ret)
  172. + module_put(THIS_MODULE);
  173. +
  174. + return ret;
  175. +}
  176. +
  177. +static void vfio_fsl_mc_release(void *device_data)
  178. +{
  179. + struct vfio_fsl_mc_device *vdev = device_data;
  180. + struct fsl_mc_device *mc_dev = vdev->mc_dev;
  181. +
  182. + mutex_lock(&driver_lock);
  183. +
  184. + if (!(--vdev->refcnt)) {
  185. + vfio_fsl_mc_regions_cleanup(vdev);
  186. + vfio_fsl_mc_irqs_cleanup(vdev);
  187. + }
  188. +
  189. + if (strcmp(mc_dev->obj_desc.type, "dprc") == 0)
  190. + dprc_reset_container(mc_dev->mc_io, 0, mc_dev->mc_handle,
  191. + mc_dev->obj_desc.id);
  192. +
  193. + mutex_unlock(&driver_lock);
  194. +
  195. + module_put(THIS_MODULE);
  196. +}
  197. +
  198. +static long vfio_fsl_mc_ioctl(void *device_data, unsigned int cmd,
  199. + unsigned long arg)
  200. +{
  201. + struct vfio_fsl_mc_device *vdev = device_data;
  202. + struct fsl_mc_device *mc_dev = vdev->mc_dev;
  203. + unsigned long minsz;
  204. +
  205. + if (WARN_ON(!mc_dev))
  206. + return -ENODEV;
  207. +
  208. + switch (cmd) {
  209. + case VFIO_DEVICE_GET_INFO:
  210. + {
  211. + struct vfio_device_info info;
  212. +
  213. + minsz = offsetofend(struct vfio_device_info, num_irqs);
  214. +
  215. + if (copy_from_user(&info, (void __user *)arg, minsz))
  216. + return -EFAULT;
  217. +
  218. + if (info.argsz < minsz)
  219. + return -EINVAL;
  220. +
  221. + info.flags = VFIO_DEVICE_FLAGS_FSL_MC;
  222. + info.num_regions = mc_dev->obj_desc.region_count;
  223. + info.num_irqs = mc_dev->obj_desc.irq_count;
  224. +
  225. + return copy_to_user((void __user *)arg, &info, minsz);
  226. + }
  227. + case VFIO_DEVICE_GET_REGION_INFO:
  228. + {
  229. + struct vfio_region_info info;
  230. +
  231. + minsz = offsetofend(struct vfio_region_info, offset);
  232. +
  233. + if (copy_from_user(&info, (void __user *)arg, minsz))
  234. + return -EFAULT;
  235. +
  236. + if (info.argsz < minsz)
  237. + return -EINVAL;
  238. +
  239. + if (info.index >= vdev->num_regions)
  240. + return -EINVAL;
  241. +
  242. + /* map offset to the physical address */
  243. + info.offset = VFIO_FSL_MC_INDEX_TO_OFFSET(info.index);
  244. + info.size = vdev->regions[info.index].size;
  245. + info.flags = vdev->regions[info.index].flags;
  246. +
  247. + return copy_to_user((void __user *)arg, &info, minsz);
  248. + }
  249. + case VFIO_DEVICE_GET_IRQ_INFO:
  250. + {
  251. + struct vfio_irq_info info;
  252. +
  253. + minsz = offsetofend(struct vfio_irq_info, count);
  254. + if (copy_from_user(&info, (void __user *)arg, minsz))
  255. + return -EFAULT;
  256. +
  257. + if (info.argsz < minsz)
  258. + return -EINVAL;
  259. +
  260. + if (info.index >= mc_dev->obj_desc.irq_count)
  261. + return -EINVAL;
  262. +
  263. + if (vdev->mc_irqs != NULL) {
  264. + info.flags = vdev->mc_irqs[info.index].flags;
  265. + info.count = vdev->mc_irqs[info.index].count;
  266. + } else {
  267. + /*
  268. + * If IRQs are not initialized then these can not
  269. + * be configuted and used by user-space/
  270. + */
  271. + info.flags = 0;
  272. + info.count = 0;
  273. + }
  274. +
  275. + return copy_to_user((void __user *)arg, &info, minsz);
  276. + }
  277. + case VFIO_DEVICE_SET_IRQS:
  278. + {
  279. + struct vfio_irq_set hdr;
  280. + u8 *data = NULL;
  281. + int ret = 0;
  282. +
  283. + minsz = offsetofend(struct vfio_irq_set, count);
  284. +
  285. + if (copy_from_user(&hdr, (void __user *)arg, minsz))
  286. + return -EFAULT;
  287. +
  288. + if (hdr.argsz < minsz)
  289. + return -EINVAL;
  290. +
  291. + if (hdr.index >= mc_dev->obj_desc.irq_count)
  292. + return -EINVAL;
  293. +
  294. + if (hdr.start != 0 || hdr.count > 1)
  295. + return -EINVAL;
  296. +
  297. + if (hdr.count == 0 &&
  298. + (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE) ||
  299. + !(hdr.flags & VFIO_IRQ_SET_ACTION_TRIGGER)))
  300. + return -EINVAL;
  301. +
  302. + if (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
  303. + VFIO_IRQ_SET_ACTION_TYPE_MASK))
  304. + return -EINVAL;
  305. +
  306. + if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
  307. + size_t size;
  308. +
  309. + if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
  310. + size = sizeof(uint8_t);
  311. + else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
  312. + size = sizeof(int32_t);
  313. + else
  314. + return -EINVAL;
  315. +
  316. + if (hdr.argsz - minsz < hdr.count * size)
  317. + return -EINVAL;
  318. +
  319. + data = memdup_user((void __user *)(arg + minsz),
  320. + hdr.count * size);
  321. + if (IS_ERR(data))
  322. + return PTR_ERR(data);
  323. + }
  324. +
  325. + ret = vfio_fsl_mc_set_irqs_ioctl(vdev, hdr.flags,
  326. + hdr.index, hdr.start,
  327. + hdr.count, data);
  328. + return ret;
  329. + }
  330. + case VFIO_DEVICE_RESET:
  331. + {
  332. + return -EINVAL;
  333. + }
  334. + default:
  335. + return -EINVAL;
  336. + }
  337. +}
  338. +
  339. +static ssize_t vfio_fsl_mc_read(void *device_data, char __user *buf,
  340. + size_t count, loff_t *ppos)
  341. +{
  342. + struct vfio_fsl_mc_device *vdev = device_data;
  343. + unsigned int index = VFIO_FSL_MC_OFFSET_TO_INDEX(*ppos);
  344. + loff_t off = *ppos & VFIO_FSL_MC_OFFSET_MASK;
  345. + struct vfio_fsl_mc_region *region;
  346. + uint64_t data[8];
  347. + int i;
  348. +
  349. + /* Read ioctl supported only for DPRC and DPMCP device */
  350. + if (strcmp(vdev->mc_dev->obj_desc.type, "dprc") &&
  351. + strcmp(vdev->mc_dev->obj_desc.type, "dpmcp"))
  352. + return -EINVAL;
  353. +
  354. + if (index >= vdev->num_regions)
  355. + return -EINVAL;
  356. +
  357. + region = &vdev->regions[index];
  358. +
  359. + if (!(region->flags & VFIO_REGION_INFO_FLAG_READ))
  360. + return -EINVAL;
  361. +
  362. + if (!region->type & VFIO_FSL_MC_REGION_TYPE_MMIO)
  363. + return -EINVAL;
  364. +
  365. + if (!region->ioaddr) {
  366. + region->ioaddr = ioremap_nocache(region->addr, region->size);
  367. + if (!region->ioaddr)
  368. + return -ENOMEM;
  369. + }
  370. +
  371. + if (count != 64 || off != 0)
  372. + return -EINVAL;
  373. +
  374. + for (i = 7; i >= 0; i--)
  375. + data[i] = readq(region->ioaddr + i * sizeof(uint64_t));
  376. +
  377. + if (copy_to_user(buf, data, 64))
  378. + return -EFAULT;
  379. +
  380. + return count;
  381. +}
  382. +
  383. +#define MC_CMD_COMPLETION_TIMEOUT_MS 5000
  384. +#define MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS 500
  385. +
  386. +static int vfio_fsl_mc_dprc_wait_for_response(void __iomem *ioaddr)
  387. +{
  388. + enum mc_cmd_status status;
  389. + unsigned long timeout_usecs = MC_CMD_COMPLETION_TIMEOUT_MS * 1000;
  390. +
  391. + for (;;) {
  392. + u64 header;
  393. + struct mc_cmd_header *resp_hdr;
  394. +
  395. + __iormb();
  396. + header = readq(ioaddr);
  397. + __iormb();
  398. +
  399. + resp_hdr = (struct mc_cmd_header *)&header;
  400. + status = (enum mc_cmd_status)resp_hdr->status;
  401. + if (status != MC_CMD_STATUS_READY)
  402. + break;
  403. +
  404. + udelay(MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS);
  405. + timeout_usecs -= MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS;
  406. + if (timeout_usecs == 0)
  407. + return -ETIMEDOUT;
  408. + }
  409. +
  410. + return 0;
  411. +}
  412. +
  413. +static int vfio_fsl_mc_send_command(void __iomem *ioaddr, uint64_t *cmd_data)
  414. +{
  415. + int i;
  416. +
  417. + /* Write at command header in the end */
  418. + for (i = 7; i >= 0; i--)
  419. + writeq(cmd_data[i], ioaddr + i * sizeof(uint64_t));
  420. +
  421. + /* Wait for response before returning to user-space
  422. + * This can be optimized in future to even prepare response
  423. + * before returning to user-space and avoid read ioctl.
  424. + */
  425. + return vfio_fsl_mc_dprc_wait_for_response(ioaddr);
  426. +}
  427. +
  428. +static int vfio_handle_dprc_commands(void __iomem *ioaddr, uint64_t *cmd_data)
  429. +{
  430. + uint64_t cmd_hdr = cmd_data[0];
  431. + int cmd = (cmd_hdr >> 52) & 0xfff;
  432. +
  433. + switch (cmd) {
  434. + case DPRC_CMDID_OPEN:
  435. + default:
  436. + return vfio_fsl_mc_send_command(ioaddr, cmd_data);
  437. + }
  438. +
  439. + return 0;
  440. +}
  441. +
  442. +static ssize_t vfio_fsl_mc_write(void *device_data, const char __user *buf,
  443. + size_t count, loff_t *ppos)
  444. +{
  445. + struct vfio_fsl_mc_device *vdev = device_data;
  446. + unsigned int index = VFIO_FSL_MC_OFFSET_TO_INDEX(*ppos);
  447. + loff_t off = *ppos & VFIO_FSL_MC_OFFSET_MASK;
  448. + struct vfio_fsl_mc_region *region;
  449. + uint64_t data[8];
  450. + int ret;
  451. +
  452. + /* Write ioctl supported only for DPRC and DPMCP device */
  453. + if (strcmp(vdev->mc_dev->obj_desc.type, "dprc") &&
  454. + strcmp(vdev->mc_dev->obj_desc.type, "dpmcp"))
  455. + return -EINVAL;
  456. +
  457. + if (index >= vdev->num_regions)
  458. + return -EINVAL;
  459. +
  460. + region = &vdev->regions[index];
  461. +
  462. + if (!(region->flags & VFIO_REGION_INFO_FLAG_WRITE))
  463. + return -EINVAL;
  464. +
  465. + if (!region->type & VFIO_FSL_MC_REGION_TYPE_MMIO)
  466. + return -EINVAL;
  467. +
  468. + if (!region->ioaddr) {
  469. + region->ioaddr = ioremap_nocache(region->addr, region->size);
  470. + if (!region->ioaddr)
  471. + return -ENOMEM;
  472. + }
  473. +
  474. + if (count != 64 || off != 0)
  475. + return -EINVAL;
  476. +
  477. + if (copy_from_user(&data, buf, 64))
  478. + return -EFAULT;
  479. +
  480. + ret = vfio_handle_dprc_commands(region->ioaddr, data);
  481. + if (ret)
  482. + return ret;
  483. +
  484. + return count;
  485. +}
  486. +
  487. +static int vfio_fsl_mc_mmap_mmio(struct vfio_fsl_mc_region region,
  488. + struct vm_area_struct *vma)
  489. +{
  490. + u64 size = vma->vm_end - vma->vm_start;
  491. + u64 pgoff, base;
  492. +
  493. + pgoff = vma->vm_pgoff &
  494. + ((1U << (VFIO_FSL_MC_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
  495. + base = pgoff << PAGE_SHIFT;
  496. +
  497. + if (region.size < PAGE_SIZE || base + size > region.size)
  498. + return -EINVAL;
  499. + /*
  500. + * Set the REGION_TYPE_CACHEABLE (QBman CENA regs) to be the
  501. + * cache inhibited area of the portal to avoid coherency issues
  502. + * if a user migrates to another core.
  503. + */
  504. + if (region.type & VFIO_FSL_MC_REGION_TYPE_CACHEABLE)
  505. + vma->vm_page_prot = pgprot_cached_ns(vma->vm_page_prot);
  506. + else
  507. + vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  508. +
  509. + vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
  510. +
  511. + return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  512. + size, vma->vm_page_prot);
  513. +}
  514. +
  515. +/* Allows mmaping fsl_mc device regions in assigned DPRC */
  516. +static int vfio_fsl_mc_mmap(void *device_data, struct vm_area_struct *vma)
  517. +{
  518. + struct vfio_fsl_mc_device *vdev = device_data;
  519. + struct fsl_mc_device *mc_dev = vdev->mc_dev;
  520. + unsigned long size, addr;
  521. + int index;
  522. +
  523. + index = vma->vm_pgoff >> (VFIO_FSL_MC_OFFSET_SHIFT - PAGE_SHIFT);
  524. +
  525. + if (vma->vm_end < vma->vm_start)
  526. + return -EINVAL;
  527. + if (vma->vm_start & ~PAGE_MASK)
  528. + return -EINVAL;
  529. + if (vma->vm_end & ~PAGE_MASK)
  530. + return -EINVAL;
  531. + if (!(vma->vm_flags & VM_SHARED))
  532. + return -EINVAL;
  533. + if (index >= vdev->num_regions)
  534. + return -EINVAL;
  535. +
  536. + if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP))
  537. + return -EINVAL;
  538. +
  539. + if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ)
  540. + && (vma->vm_flags & VM_READ))
  541. + return -EINVAL;
  542. +
  543. + if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
  544. + && (vma->vm_flags & VM_WRITE))
  545. + return -EINVAL;
  546. +
  547. + addr = vdev->regions[index].addr;
  548. + size = vdev->regions[index].size;
  549. +
  550. + vma->vm_private_data = mc_dev;
  551. +
  552. + if (vdev->regions[index].type & VFIO_FSL_MC_REGION_TYPE_MMIO)
  553. + return vfio_fsl_mc_mmap_mmio(vdev->regions[index], vma);
  554. +
  555. + return -EFAULT;
  556. +}
  557. +
  558. +static const struct vfio_device_ops vfio_fsl_mc_ops = {
  559. + .name = "vfio-fsl-mc",
  560. + .open = vfio_fsl_mc_open,
  561. + .release = vfio_fsl_mc_release,
  562. + .ioctl = vfio_fsl_mc_ioctl,
  563. + .read = vfio_fsl_mc_read,
  564. + .write = vfio_fsl_mc_write,
  565. + .mmap = vfio_fsl_mc_mmap,
  566. +};
  567. +
  568. +static int vfio_fsl_mc_initialize_dprc(struct vfio_fsl_mc_device *vdev)
  569. +{
  570. + struct device *root_dprc_dev;
  571. + struct fsl_mc_device *mc_dev = vdev->mc_dev;
  572. + struct device *dev = &mc_dev->dev;
  573. + struct fsl_mc_bus *mc_bus;
  574. + struct irq_domain *mc_msi_domain;
  575. + unsigned int irq_count;
  576. + int ret;
  577. +
  578. + /* device must be DPRC */
  579. + if (strcmp(mc_dev->obj_desc.type, "dprc"))
  580. + return -EINVAL;
  581. +
  582. + /* mc_io must be un-initialized */
  583. + WARN_ON(mc_dev->mc_io);
  584. +
  585. + /* allocate a portal from the root DPRC for vfio use */
  586. + fsl_mc_get_root_dprc(dev, &root_dprc_dev);
  587. + if (WARN_ON(!root_dprc_dev))
  588. + return -EINVAL;
  589. +
  590. + ret = fsl_mc_portal_allocate(to_fsl_mc_device(root_dprc_dev),
  591. + FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
  592. + &mc_dev->mc_io);
  593. + if (ret < 0)
  594. + goto clean_msi_domain;
  595. +
  596. + /* Reset MCP before move on */
  597. + ret = fsl_mc_portal_reset(mc_dev->mc_io);
  598. + if (ret < 0) {
  599. + dev_err(dev, "dprc portal reset failed: error = %d\n", ret);
  600. + goto free_mc_portal;
  601. + }
  602. +
  603. + /* MSI domain set up */
  604. + ret = fsl_mc_find_msi_domain(root_dprc_dev->parent, &mc_msi_domain);
  605. + if (ret < 0)
  606. + goto free_mc_portal;
  607. +
  608. + dev_set_msi_domain(&mc_dev->dev, mc_msi_domain);
  609. +
  610. + ret = dprc_open(mc_dev->mc_io, 0, mc_dev->obj_desc.id,
  611. + &mc_dev->mc_handle);
  612. + if (ret) {
  613. + dev_err(dev, "dprc_open() failed: error = %d\n", ret);
  614. + goto free_mc_portal;
  615. + }
  616. +
  617. + /* Initialize resource pool */
  618. + fsl_mc_init_all_resource_pools(mc_dev);
  619. +
  620. + mc_bus = to_fsl_mc_bus(mc_dev);
  621. +
  622. + if (!mc_bus->irq_resources) {
  623. + irq_count = FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS;
  624. + ret = fsl_mc_populate_irq_pool(mc_bus, irq_count);
  625. + if (ret < 0) {
  626. + dev_err(dev, "%s: Failed to init irq-pool\n", __func__);
  627. + goto clean_resource_pool;
  628. + }
  629. + }
  630. +
  631. + mutex_init(&mc_bus->scan_mutex);
  632. +
  633. + mutex_lock(&mc_bus->scan_mutex);
  634. + ret = dprc_scan_objects(mc_dev, mc_dev->driver_override,
  635. + &irq_count);
  636. + mutex_unlock(&mc_bus->scan_mutex);
  637. + if (ret) {
  638. + dev_err(dev, "dprc_scan_objects() fails (%d)\n", ret);
  639. + goto clean_irq_pool;
  640. + }
  641. +
  642. + if (irq_count > FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS) {
  643. + dev_warn(&mc_dev->dev,
  644. + "IRQs needed (%u) exceed IRQs preallocated (%u)\n",
  645. + irq_count, FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS);
  646. + }
  647. +
  648. + return 0;
  649. +
  650. +clean_irq_pool:
  651. + fsl_mc_cleanup_irq_pool(mc_bus);
  652. +
  653. +clean_resource_pool:
  654. + fsl_mc_cleanup_all_resource_pools(mc_dev);
  655. + dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
  656. +
  657. +free_mc_portal:
  658. + fsl_mc_portal_free(mc_dev->mc_io);
  659. +
  660. +clean_msi_domain:
  661. + dev_set_msi_domain(&mc_dev->dev, NULL);
  662. +
  663. + return ret;
  664. +}
  665. +
  666. +static int vfio_fsl_mc_device_remove(struct device *dev, void *data)
  667. +{
  668. + struct fsl_mc_device *mc_dev;
  669. +
  670. + WARN_ON(dev == NULL);
  671. +
  672. + mc_dev = to_fsl_mc_device(dev);
  673. + if (WARN_ON(mc_dev == NULL))
  674. + return -ENODEV;
  675. +
  676. + fsl_mc_device_remove(mc_dev);
  677. + return 0;
  678. +}
  679. +
  680. +static void vfio_fsl_mc_cleanup_dprc(struct vfio_fsl_mc_device *vdev)
  681. +{
  682. + struct fsl_mc_device *mc_dev = vdev->mc_dev;
  683. + struct fsl_mc_bus *mc_bus;
  684. +
  685. + /* device must be DPRC */
  686. + if (strcmp(mc_dev->obj_desc.type, "dprc"))
  687. + return;
  688. +
  689. + device_for_each_child(&mc_dev->dev, NULL, vfio_fsl_mc_device_remove);
  690. +
  691. + mc_bus = to_fsl_mc_bus(mc_dev);
  692. + if (dev_get_msi_domain(&mc_dev->dev))
  693. + fsl_mc_cleanup_irq_pool(mc_bus);
  694. +
  695. + dev_set_msi_domain(&mc_dev->dev, NULL);
  696. +
  697. + fsl_mc_cleanup_all_resource_pools(mc_dev);
  698. + dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
  699. + fsl_mc_portal_free(mc_dev->mc_io);
  700. +}
  701. +
  702. +static int vfio_fsl_mc_probe(struct fsl_mc_device *mc_dev)
  703. +{
  704. + struct iommu_group *group;
  705. + struct vfio_fsl_mc_device *vdev;
  706. + struct device *dev = &mc_dev->dev;
  707. + int ret;
  708. +
  709. + group = vfio_iommu_group_get(dev);
  710. + if (!group) {
  711. + dev_err(dev, "%s: VFIO: No IOMMU group\n", __func__);
  712. + return -EINVAL;
  713. + }
  714. +
  715. + vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  716. + if (!vdev) {
  717. + vfio_iommu_group_put(group, dev);
  718. + return -ENOMEM;
  719. + }
  720. +
  721. + vdev->mc_dev = mc_dev;
  722. +
  723. + ret = vfio_add_group_dev(dev, &vfio_fsl_mc_ops, vdev);
  724. + if (ret) {
  725. + dev_err(dev, "%s: Failed to add to vfio group\n", __func__);
  726. + goto free_vfio_device;
  727. + }
  728. +
  729. + /* DPRC container scanned and it's chilren bound with vfio driver */
  730. + if (strcmp(mc_dev->obj_desc.type, "dprc") == 0) {
  731. + ret = vfio_fsl_mc_initialize_dprc(vdev);
  732. + if (ret) {
  733. + vfio_del_group_dev(dev);
  734. + goto free_vfio_device;
  735. + }
  736. + } else {
  737. + struct fsl_mc_device *mc_bus_dev;
  738. +
  739. + /* Non-dprc devices share mc_io from the parent dprc */
  740. + mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
  741. + if (mc_bus_dev == NULL) {
  742. + vfio_del_group_dev(dev);
  743. + goto free_vfio_device;
  744. + }
  745. +
  746. + mc_dev->mc_io = mc_bus_dev->mc_io;
  747. +
  748. + /* Inherit parent MSI domain */
  749. + dev_set_msi_domain(&mc_dev->dev,
  750. + dev_get_msi_domain(mc_dev->dev.parent));
  751. + }
  752. + return 0;
  753. +
  754. +free_vfio_device:
  755. + kfree(vdev);
  756. + vfio_iommu_group_put(group, dev);
  757. + return ret;
  758. +}
  759. +
  760. +static int vfio_fsl_mc_remove(struct fsl_mc_device *mc_dev)
  761. +{
  762. + struct vfio_fsl_mc_device *vdev;
  763. + struct device *dev = &mc_dev->dev;
  764. +
  765. + vdev = vfio_del_group_dev(dev);
  766. + if (!vdev)
  767. + return -EINVAL;
  768. +
  769. + if (strcmp(mc_dev->obj_desc.type, "dprc") == 0)
  770. + vfio_fsl_mc_cleanup_dprc(vdev);
  771. + else
  772. + dev_set_msi_domain(&mc_dev->dev, NULL);
  773. +
  774. + mc_dev->mc_io = NULL;
  775. +
  776. + vfio_iommu_group_put(mc_dev->dev.iommu_group, dev);
  777. + kfree(vdev);
  778. +
  779. + return 0;
  780. +}
  781. +
  782. +/*
  783. + * vfio-fsl_mc is a meta-driver, so use driver_override interface to
  784. + * bind a fsl_mc container with this driver and match_id_table is NULL.
  785. + */
  786. +static struct fsl_mc_driver vfio_fsl_mc_driver = {
  787. + .probe = vfio_fsl_mc_probe,
  788. + .remove = vfio_fsl_mc_remove,
  789. + .match_id_table = NULL,
  790. + .driver = {
  791. + .name = "vfio-fsl-mc",
  792. + .owner = THIS_MODULE,
  793. + },
  794. +};
  795. +
  796. +static int __init vfio_fsl_mc_driver_init(void)
  797. +{
  798. + return fsl_mc_driver_register(&vfio_fsl_mc_driver);
  799. +}
  800. +
  801. +static void __exit vfio_fsl_mc_driver_exit(void)
  802. +{
  803. + fsl_mc_driver_unregister(&vfio_fsl_mc_driver);
  804. +}
  805. +
  806. +module_init(vfio_fsl_mc_driver_init);
  807. +module_exit(vfio_fsl_mc_driver_exit);
  808. +
  809. +MODULE_VERSION(DRIVER_VERSION);
  810. +MODULE_LICENSE("GPL v2");
  811. +MODULE_AUTHOR(DRIVER_AUTHOR);
  812. +MODULE_DESCRIPTION(DRIVER_DESC);
  813. --- /dev/null
  814. +++ b/drivers/vfio/fsl-mc/vfio_fsl_mc_intr.c
  815. @@ -0,0 +1,199 @@
  816. +/*
  817. + * Freescale Management Complex (MC) device passthrough using VFIO
  818. + *
  819. + * Copyright (C) 2013-2016 Freescale Semiconductor, Inc.
  820. + * Author: Bharat Bhushan <[email protected]>
  821. + *
  822. + * This file is licensed under the terms of the GNU General Public
  823. + * License version 2. This program is licensed "as is" without any
  824. + * warranty of any kind, whether express or implied.
  825. + */
  826. +
  827. +#include <linux/vfio.h>
  828. +#include <linux/slab.h>
  829. +#include <linux/types.h>
  830. +#include <linux/eventfd.h>
  831. +#include <linux/msi.h>
  832. +
  833. +#include "linux/fsl/mc.h"
  834. +#include "vfio_fsl_mc_private.h"
  835. +
  836. +static irqreturn_t vfio_fsl_mc_irq_handler(int irq_num, void *arg)
  837. +{
  838. + struct vfio_fsl_mc_irq *mc_irq = (struct vfio_fsl_mc_irq *)arg;
  839. +
  840. + eventfd_signal(mc_irq->trigger, 1);
  841. + return IRQ_HANDLED;
  842. +}
  843. +
  844. +static int vfio_fsl_mc_irq_mask(struct vfio_fsl_mc_device *vdev,
  845. + unsigned int index, unsigned int start,
  846. + unsigned int count, uint32_t flags,
  847. + void *data)
  848. +{
  849. + return -EINVAL;
  850. +}
  851. +
  852. +static int vfio_fsl_mc_irq_unmask(struct vfio_fsl_mc_device *vdev,
  853. + unsigned int index, unsigned int start,
  854. + unsigned int count, uint32_t flags,
  855. + void *data)
  856. +{
  857. + return -EINVAL;
  858. +}
  859. +
  860. +static int vfio_set_trigger(struct vfio_fsl_mc_device *vdev,
  861. + int index, int fd)
  862. +{
  863. + struct vfio_fsl_mc_irq *irq = &vdev->mc_irqs[index];
  864. + struct eventfd_ctx *trigger;
  865. + int hwirq;
  866. + int ret;
  867. +
  868. + hwirq = vdev->mc_dev->irqs[index]->msi_desc->irq;
  869. + if (irq->trigger) {
  870. + free_irq(hwirq, irq);
  871. + kfree(irq->name);
  872. + eventfd_ctx_put(irq->trigger);
  873. + irq->trigger = NULL;
  874. + }
  875. +
  876. + if (fd < 0) /* Disable only */
  877. + return 0;
  878. +
  879. + irq->name = kasprintf(GFP_KERNEL, "vfio-irq[%d](%s)",
  880. + hwirq, dev_name(&vdev->mc_dev->dev));
  881. + if (!irq->name)
  882. + return -ENOMEM;
  883. +
  884. + trigger = eventfd_ctx_fdget(fd);
  885. + if (IS_ERR(trigger)) {
  886. + kfree(irq->name);
  887. + return PTR_ERR(trigger);
  888. + }
  889. +
  890. + irq->trigger = trigger;
  891. +
  892. + ret = request_irq(hwirq, vfio_fsl_mc_irq_handler, 0,
  893. + irq->name, irq);
  894. + if (ret) {
  895. + kfree(irq->name);
  896. + eventfd_ctx_put(trigger);
  897. + irq->trigger = NULL;
  898. + return ret;
  899. + }
  900. +
  901. + return 0;
  902. +}
  903. +
  904. +int vfio_fsl_mc_irqs_init(struct vfio_fsl_mc_device *vdev)
  905. +{
  906. + struct fsl_mc_device *mc_dev = vdev->mc_dev;
  907. + struct vfio_fsl_mc_irq *mc_irq;
  908. + int irq_count;
  909. + int ret, i;
  910. +
  911. + /* Device does not support any interrupt */
  912. + if (mc_dev->obj_desc.irq_count == 0)
  913. + return 0;
  914. +
  915. + irq_count = mc_dev->obj_desc.irq_count;
  916. +
  917. + mc_irq = kcalloc(irq_count, sizeof(*mc_irq), GFP_KERNEL);
  918. + if (mc_irq == NULL)
  919. + return -ENOMEM;
  920. +
  921. + /* Allocate IRQs */
  922. + ret = fsl_mc_allocate_irqs(mc_dev);
  923. + if (ret) {
  924. + kfree(mc_irq);
  925. + return ret;
  926. + }
  927. +
  928. + for (i = 0; i < irq_count; i++) {
  929. + mc_irq[i].count = 1;
  930. + mc_irq[i].flags = VFIO_IRQ_INFO_EVENTFD;
  931. + }
  932. +
  933. + vdev->mc_irqs = mc_irq;
  934. +
  935. + return 0;
  936. +}
  937. +
  938. +/* Free All IRQs for the given MC object */
  939. +void vfio_fsl_mc_irqs_cleanup(struct vfio_fsl_mc_device *vdev)
  940. +{
  941. + struct fsl_mc_device *mc_dev = vdev->mc_dev;
  942. + int irq_count = mc_dev->obj_desc.irq_count;
  943. + int i;
  944. +
  945. + /* Device does not support any interrupt */
  946. + if (mc_dev->obj_desc.irq_count == 0)
  947. + return;
  948. +
  949. + for (i = 0; i < irq_count; i++)
  950. + vfio_set_trigger(vdev, i, -1);
  951. +
  952. + fsl_mc_free_irqs(mc_dev);
  953. + kfree(vdev->mc_irqs);
  954. +}
  955. +
  956. +static int vfio_fsl_mc_set_irq_trigger(struct vfio_fsl_mc_device *vdev,
  957. + unsigned int index, unsigned int start,
  958. + unsigned int count, uint32_t flags,
  959. + void *data)
  960. +{
  961. + struct vfio_fsl_mc_irq *irq = &vdev->mc_irqs[index];
  962. + int hwirq;
  963. +
  964. + if (!count && (flags & VFIO_IRQ_SET_DATA_NONE))
  965. + return vfio_set_trigger(vdev, index, -1);
  966. +
  967. + if (start != 0 || count != 1)
  968. + return -EINVAL;
  969. +
  970. + if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
  971. + int32_t fd = *(int32_t *)data;
  972. +
  973. + return vfio_set_trigger(vdev, index, fd);
  974. + }
  975. +
  976. + hwirq = vdev->mc_dev->irqs[index]->msi_desc->irq;
  977. +
  978. + if (flags & VFIO_IRQ_SET_DATA_NONE) {
  979. + vfio_fsl_mc_irq_handler(hwirq, irq);
  980. +
  981. + } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
  982. + uint8_t trigger = *(uint8_t *)data;
  983. +
  984. + if (trigger)
  985. + vfio_fsl_mc_irq_handler(hwirq, irq);
  986. + }
  987. +
  988. + return 0;
  989. +}
  990. +
  991. +int vfio_fsl_mc_set_irqs_ioctl(struct vfio_fsl_mc_device *vdev,
  992. + uint32_t flags, unsigned int index,
  993. + unsigned int start, unsigned int count,
  994. + void *data)
  995. +{
  996. + int ret = -ENOTTY;
  997. +
  998. + switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
  999. + case VFIO_IRQ_SET_ACTION_MASK:
  1000. + ret = vfio_fsl_mc_irq_mask(vdev, index, start, count,
  1001. + flags, data);
  1002. + break;
  1003. + case VFIO_IRQ_SET_ACTION_UNMASK:
  1004. + ret = vfio_fsl_mc_irq_unmask(vdev, index, start, count,
  1005. + flags, data);
  1006. + break;
  1007. + case VFIO_IRQ_SET_ACTION_TRIGGER:
  1008. + ret = vfio_fsl_mc_set_irq_trigger(vdev, index, start,
  1009. + count, flags, data);
  1010. + break;
  1011. + }
  1012. +
  1013. + return ret;
  1014. +}
  1015. --- /dev/null
  1016. +++ b/drivers/vfio/fsl-mc/vfio_fsl_mc_private.h
  1017. @@ -0,0 +1,55 @@
  1018. +/*
  1019. + * Freescale Management Complex VFIO private declarations
  1020. + *
  1021. + * Copyright (C) 2013-2016 Freescale Semiconductor, Inc.
  1022. + * Copyright 2016 NXP
  1023. + * Author: Bharat Bhushan <[email protected]>
  1024. + *
  1025. + * This file is licensed under the terms of the GNU General Public
  1026. + * License version 2. This program is licensed "as is" without any
  1027. + * warranty of any kind, whether express or implied.
  1028. + */
  1029. +
  1030. +#ifndef VFIO_FSL_MC_PRIVATE_H
  1031. +#define VFIO_FSL_MC_PRIVATE_H
  1032. +
  1033. +#define VFIO_FSL_MC_OFFSET_SHIFT 40
  1034. +#define VFIO_FSL_MC_OFFSET_MASK (((u64)(1) << VFIO_FSL_MC_OFFSET_SHIFT) - 1)
  1035. +
  1036. +#define VFIO_FSL_MC_OFFSET_TO_INDEX(off) (off >> VFIO_FSL_MC_OFFSET_SHIFT)
  1037. +
  1038. +#define VFIO_FSL_MC_INDEX_TO_OFFSET(index) \
  1039. + ((u64)(index) << VFIO_FSL_MC_OFFSET_SHIFT)
  1040. +
  1041. +struct vfio_fsl_mc_irq {
  1042. + u32 flags;
  1043. + u32 count;
  1044. + struct eventfd_ctx *trigger;
  1045. + char *name;
  1046. +};
  1047. +
  1048. +struct vfio_fsl_mc_region {
  1049. + u32 flags;
  1050. +#define VFIO_FSL_MC_REGION_TYPE_MMIO 1
  1051. +#define VFIO_FSL_MC_REGION_TYPE_CACHEABLE 2
  1052. + u32 type;
  1053. + u64 addr;
  1054. + resource_size_t size;
  1055. + void __iomem *ioaddr;
  1056. +};
  1057. +
  1058. +struct vfio_fsl_mc_device {
  1059. + struct fsl_mc_device *mc_dev;
  1060. + int refcnt;
  1061. + u32 num_regions;
  1062. + struct vfio_fsl_mc_region *regions;
  1063. + struct vfio_fsl_mc_irq *mc_irqs;
  1064. +};
  1065. +
  1066. +int vfio_fsl_mc_irqs_init(struct vfio_fsl_mc_device *vdev);
  1067. +void vfio_fsl_mc_irqs_cleanup(struct vfio_fsl_mc_device *vdev);
  1068. +int vfio_fsl_mc_set_irqs_ioctl(struct vfio_fsl_mc_device *vdev,
  1069. + uint32_t flags, unsigned int index,
  1070. + unsigned int start, unsigned int count,
  1071. + void *data);
  1072. +#endif /* VFIO_PCI_PRIVATE_H */
  1073. --- a/drivers/vfio/vfio_iommu_type1.c
  1074. +++ b/drivers/vfio/vfio_iommu_type1.c
  1075. @@ -36,6 +36,8 @@
  1076. #include <linux/uaccess.h>
  1077. #include <linux/vfio.h>
  1078. #include <linux/workqueue.h>
  1079. +#include <linux/dma-iommu.h>
  1080. +#include <linux/irqdomain.h>
  1081. #define DRIVER_VERSION "0.2"
  1082. #define DRIVER_AUTHOR "Alex Williamson <[email protected]>"
  1083. @@ -720,6 +722,27 @@ static void vfio_test_domain_fgsp(struct
  1084. __free_pages(pages, order);
  1085. }
  1086. +static bool vfio_iommu_has_sw_msi(struct iommu_group *group, phys_addr_t *base)
  1087. +{
  1088. + struct list_head group_resv_regions;
  1089. + struct iommu_resv_region *region, *next;
  1090. + bool ret = false;
  1091. +
  1092. + INIT_LIST_HEAD(&group_resv_regions);
  1093. + iommu_get_group_resv_regions(group, &group_resv_regions);
  1094. + list_for_each_entry(region, &group_resv_regions, list) {
  1095. + if (region->type == IOMMU_RESV_SW_MSI) {
  1096. + *base = region->start;
  1097. + ret = true;
  1098. + goto out;
  1099. + }
  1100. + }
  1101. +out:
  1102. + list_for_each_entry_safe(region, next, &group_resv_regions, list)
  1103. + kfree(region);
  1104. + return ret;
  1105. +}
  1106. +
  1107. static int vfio_iommu_type1_attach_group(void *iommu_data,
  1108. struct iommu_group *iommu_group)
  1109. {
  1110. @@ -728,6 +751,8 @@ static int vfio_iommu_type1_attach_group
  1111. struct vfio_domain *domain, *d;
  1112. struct bus_type *bus = NULL;
  1113. int ret;
  1114. + bool resv_msi, msi_remap;
  1115. + phys_addr_t resv_msi_base;
  1116. mutex_lock(&iommu->lock);
  1117. @@ -774,11 +799,15 @@ static int vfio_iommu_type1_attach_group
  1118. if (ret)
  1119. goto out_domain;
  1120. + resv_msi = vfio_iommu_has_sw_msi(iommu_group, &resv_msi_base);
  1121. +
  1122. INIT_LIST_HEAD(&domain->group_list);
  1123. list_add(&group->next, &domain->group_list);
  1124. - if (!allow_unsafe_interrupts &&
  1125. - !iommu_capable(bus, IOMMU_CAP_INTR_REMAP)) {
  1126. + msi_remap = resv_msi ? irq_domain_check_msi_remap() :
  1127. + iommu_capable(bus, IOMMU_CAP_INTR_REMAP);
  1128. +
  1129. + if (!allow_unsafe_interrupts && !msi_remap) {
  1130. pr_warn("%s: No interrupt remapping support. Use the module param \"allow_unsafe_interrupts\" to enable VFIO IOMMU support on this platform\n",
  1131. __func__);
  1132. ret = -EPERM;
  1133. @@ -820,6 +849,12 @@ static int vfio_iommu_type1_attach_group
  1134. if (ret)
  1135. goto out_detach;
  1136. + if (resv_msi) {
  1137. + ret = iommu_get_msi_cookie(domain->domain, resv_msi_base);
  1138. + if (ret)
  1139. + goto out_detach;
  1140. + }
  1141. +
  1142. list_add(&domain->next, &iommu->domain_list);
  1143. mutex_unlock(&iommu->lock);
  1144. --- a/include/uapi/linux/vfio.h
  1145. +++ b/include/uapi/linux/vfio.h
  1146. @@ -198,6 +198,7 @@ struct vfio_device_info {
  1147. #define VFIO_DEVICE_FLAGS_PCI (1 << 1) /* vfio-pci device */
  1148. #define VFIO_DEVICE_FLAGS_PLATFORM (1 << 2) /* vfio-platform device */
  1149. #define VFIO_DEVICE_FLAGS_AMBA (1 << 3) /* vfio-amba device */
  1150. +#define VFIO_DEVICE_FLAGS_FSL_MC (1 << 5) /* vfio-fsl-mc device */
  1151. __u32 num_regions; /* Max region index + 1 */
  1152. __u32 num_irqs; /* Max IRQ index + 1 */
  1153. };