virtio-iommu.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Virtio driver for the paravirtualized IOMMU
  4. *
  5. * Copyright (C) 2019 Arm Limited
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/delay.h>
  9. #include <linux/dma-map-ops.h>
  10. #include <linux/freezer.h>
  11. #include <linux/interval_tree.h>
  12. #include <linux/iommu.h>
  13. #include <linux/module.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/pci.h>
  16. #include <linux/virtio.h>
  17. #include <linux/virtio_config.h>
  18. #include <linux/virtio_ids.h>
  19. #include <linux/wait.h>
  20. #include <uapi/linux/virtio_iommu.h>
  21. #include "dma-iommu.h"
  22. #define MSI_IOVA_BASE 0x8000000
  23. #define MSI_IOVA_LENGTH 0x100000
  24. #define VIOMMU_REQUEST_VQ 0
  25. #define VIOMMU_EVENT_VQ 1
  26. #define VIOMMU_NR_VQS 2
  27. struct viommu_dev {
  28. struct iommu_device iommu;
  29. struct device *dev;
  30. struct virtio_device *vdev;
  31. struct ida domain_ids;
  32. struct virtqueue *vqs[VIOMMU_NR_VQS];
  33. spinlock_t request_lock;
  34. struct list_head requests;
  35. void *evts;
  36. /* Device configuration */
  37. struct iommu_domain_geometry geometry;
  38. u64 pgsize_bitmap;
  39. u32 first_domain;
  40. u32 last_domain;
  41. /* Supported MAP flags */
  42. u32 map_flags;
  43. u32 probe_size;
  44. };
  45. struct viommu_mapping {
  46. phys_addr_t paddr;
  47. struct interval_tree_node iova;
  48. u32 flags;
  49. };
  50. struct viommu_domain {
  51. struct iommu_domain domain;
  52. struct viommu_dev *viommu;
  53. struct mutex mutex; /* protects viommu pointer */
  54. unsigned int id;
  55. u32 map_flags;
  56. spinlock_t mappings_lock;
  57. struct rb_root_cached mappings;
  58. unsigned long nr_endpoints;
  59. bool bypass;
  60. };
  61. struct viommu_endpoint {
  62. struct device *dev;
  63. struct viommu_dev *viommu;
  64. struct viommu_domain *vdomain;
  65. struct list_head resv_regions;
  66. };
  67. struct viommu_request {
  68. struct list_head list;
  69. void *writeback;
  70. unsigned int write_offset;
  71. unsigned int len;
  72. char buf[];
  73. };
  74. #define VIOMMU_FAULT_RESV_MASK 0xffffff00
  75. struct viommu_event {
  76. union {
  77. u32 head;
  78. struct virtio_iommu_fault fault;
  79. };
  80. };
  81. #define to_viommu_domain(domain) \
  82. container_of(domain, struct viommu_domain, domain)
  83. static int viommu_get_req_errno(void *buf, size_t len)
  84. {
  85. struct virtio_iommu_req_tail *tail = buf + len - sizeof(*tail);
  86. switch (tail->status) {
  87. case VIRTIO_IOMMU_S_OK:
  88. return 0;
  89. case VIRTIO_IOMMU_S_UNSUPP:
  90. return -ENOSYS;
  91. case VIRTIO_IOMMU_S_INVAL:
  92. return -EINVAL;
  93. case VIRTIO_IOMMU_S_RANGE:
  94. return -ERANGE;
  95. case VIRTIO_IOMMU_S_NOENT:
  96. return -ENOENT;
  97. case VIRTIO_IOMMU_S_FAULT:
  98. return -EFAULT;
  99. case VIRTIO_IOMMU_S_NOMEM:
  100. return -ENOMEM;
  101. case VIRTIO_IOMMU_S_IOERR:
  102. case VIRTIO_IOMMU_S_DEVERR:
  103. default:
  104. return -EIO;
  105. }
  106. }
  107. static void viommu_set_req_status(void *buf, size_t len, int status)
  108. {
  109. struct virtio_iommu_req_tail *tail = buf + len - sizeof(*tail);
  110. tail->status = status;
  111. }
  112. static off_t viommu_get_write_desc_offset(struct viommu_dev *viommu,
  113. struct virtio_iommu_req_head *req,
  114. size_t len)
  115. {
  116. size_t tail_size = sizeof(struct virtio_iommu_req_tail);
  117. if (req->type == VIRTIO_IOMMU_T_PROBE)
  118. return len - viommu->probe_size - tail_size;
  119. return len - tail_size;
  120. }
  121. /*
  122. * __viommu_sync_req - Complete all in-flight requests
  123. *
  124. * Wait for all added requests to complete. When this function returns, all
  125. * requests that were in-flight at the time of the call have completed.
  126. */
  127. static int __viommu_sync_req(struct viommu_dev *viommu)
  128. {
  129. unsigned int len;
  130. size_t write_len;
  131. struct viommu_request *req;
  132. struct virtqueue *vq = viommu->vqs[VIOMMU_REQUEST_VQ];
  133. assert_spin_locked(&viommu->request_lock);
  134. virtqueue_kick(vq);
  135. while (!list_empty(&viommu->requests)) {
  136. len = 0;
  137. req = virtqueue_get_buf(vq, &len);
  138. if (!req)
  139. continue;
  140. if (!len)
  141. viommu_set_req_status(req->buf, req->len,
  142. VIRTIO_IOMMU_S_IOERR);
  143. write_len = req->len - req->write_offset;
  144. if (req->writeback && len == write_len)
  145. memcpy(req->writeback, req->buf + req->write_offset,
  146. write_len);
  147. list_del(&req->list);
  148. kfree(req);
  149. }
  150. return 0;
  151. }
  152. static int viommu_sync_req(struct viommu_dev *viommu)
  153. {
  154. int ret;
  155. unsigned long flags;
  156. spin_lock_irqsave(&viommu->request_lock, flags);
  157. ret = __viommu_sync_req(viommu);
  158. if (ret)
  159. dev_dbg(viommu->dev, "could not sync requests (%d)\n", ret);
  160. spin_unlock_irqrestore(&viommu->request_lock, flags);
  161. return ret;
  162. }
  163. /*
  164. * __viommu_add_request - Add one request to the queue
  165. * @buf: pointer to the request buffer
  166. * @len: length of the request buffer
  167. * @writeback: copy data back to the buffer when the request completes.
  168. *
  169. * Add a request to the queue. Only synchronize the queue if it's already full.
  170. * Otherwise don't kick the queue nor wait for requests to complete.
  171. *
  172. * When @writeback is true, data written by the device, including the request
  173. * status, is copied into @buf after the request completes. This is unsafe if
  174. * the caller allocates @buf on stack and drops the lock between add_req() and
  175. * sync_req().
  176. *
  177. * Return 0 if the request was successfully added to the queue.
  178. */
  179. static int __viommu_add_req(struct viommu_dev *viommu, void *buf, size_t len,
  180. bool writeback)
  181. {
  182. int ret;
  183. off_t write_offset;
  184. struct viommu_request *req;
  185. struct scatterlist top_sg, bottom_sg;
  186. struct scatterlist *sg[2] = { &top_sg, &bottom_sg };
  187. struct virtqueue *vq = viommu->vqs[VIOMMU_REQUEST_VQ];
  188. assert_spin_locked(&viommu->request_lock);
  189. write_offset = viommu_get_write_desc_offset(viommu, buf, len);
  190. if (write_offset <= 0)
  191. return -EINVAL;
  192. req = kzalloc(sizeof(*req) + len, GFP_ATOMIC);
  193. if (!req)
  194. return -ENOMEM;
  195. req->len = len;
  196. if (writeback) {
  197. req->writeback = buf + write_offset;
  198. req->write_offset = write_offset;
  199. }
  200. memcpy(&req->buf, buf, write_offset);
  201. sg_init_one(&top_sg, req->buf, write_offset);
  202. sg_init_one(&bottom_sg, req->buf + write_offset, len - write_offset);
  203. ret = virtqueue_add_sgs(vq, sg, 1, 1, req, GFP_ATOMIC);
  204. if (ret == -ENOSPC) {
  205. /* If the queue is full, sync and retry */
  206. if (!__viommu_sync_req(viommu))
  207. ret = virtqueue_add_sgs(vq, sg, 1, 1, req, GFP_ATOMIC);
  208. }
  209. if (ret)
  210. goto err_free;
  211. list_add_tail(&req->list, &viommu->requests);
  212. return 0;
  213. err_free:
  214. kfree(req);
  215. return ret;
  216. }
  217. static int viommu_add_req(struct viommu_dev *viommu, void *buf, size_t len)
  218. {
  219. int ret;
  220. unsigned long flags;
  221. spin_lock_irqsave(&viommu->request_lock, flags);
  222. ret = __viommu_add_req(viommu, buf, len, false);
  223. if (ret)
  224. dev_dbg(viommu->dev, "could not add request: %d\n", ret);
  225. spin_unlock_irqrestore(&viommu->request_lock, flags);
  226. return ret;
  227. }
  228. /*
  229. * Send a request and wait for it to complete. Return the request status (as an
  230. * errno)
  231. */
  232. static int viommu_send_req_sync(struct viommu_dev *viommu, void *buf,
  233. size_t len)
  234. {
  235. int ret;
  236. unsigned long flags;
  237. spin_lock_irqsave(&viommu->request_lock, flags);
  238. ret = __viommu_add_req(viommu, buf, len, true);
  239. if (ret) {
  240. dev_dbg(viommu->dev, "could not add request (%d)\n", ret);
  241. goto out_unlock;
  242. }
  243. ret = __viommu_sync_req(viommu);
  244. if (ret) {
  245. dev_dbg(viommu->dev, "could not sync requests (%d)\n", ret);
  246. /* Fall-through (get the actual request status) */
  247. }
  248. ret = viommu_get_req_errno(buf, len);
  249. out_unlock:
  250. spin_unlock_irqrestore(&viommu->request_lock, flags);
  251. return ret;
  252. }
  253. /*
  254. * viommu_add_mapping - add a mapping to the internal tree
  255. *
  256. * On success, return the new mapping. Otherwise return NULL.
  257. */
  258. static int viommu_add_mapping(struct viommu_domain *vdomain, u64 iova, u64 end,
  259. phys_addr_t paddr, u32 flags)
  260. {
  261. unsigned long irqflags;
  262. struct viommu_mapping *mapping;
  263. mapping = kzalloc(sizeof(*mapping), GFP_ATOMIC);
  264. if (!mapping)
  265. return -ENOMEM;
  266. mapping->paddr = paddr;
  267. mapping->iova.start = iova;
  268. mapping->iova.last = end;
  269. mapping->flags = flags;
  270. spin_lock_irqsave(&vdomain->mappings_lock, irqflags);
  271. interval_tree_insert(&mapping->iova, &vdomain->mappings);
  272. spin_unlock_irqrestore(&vdomain->mappings_lock, irqflags);
  273. return 0;
  274. }
  275. /*
  276. * viommu_del_mappings - remove mappings from the internal tree
  277. *
  278. * @vdomain: the domain
  279. * @iova: start of the range
  280. * @end: end of the range
  281. *
  282. * On success, returns the number of unmapped bytes
  283. */
  284. static size_t viommu_del_mappings(struct viommu_domain *vdomain,
  285. u64 iova, u64 end)
  286. {
  287. size_t unmapped = 0;
  288. unsigned long flags;
  289. struct viommu_mapping *mapping = NULL;
  290. struct interval_tree_node *node, *next;
  291. spin_lock_irqsave(&vdomain->mappings_lock, flags);
  292. next = interval_tree_iter_first(&vdomain->mappings, iova, end);
  293. while (next) {
  294. node = next;
  295. mapping = container_of(node, struct viommu_mapping, iova);
  296. next = interval_tree_iter_next(node, iova, end);
  297. /* Trying to split a mapping? */
  298. if (mapping->iova.start < iova)
  299. break;
  300. /*
  301. * Virtio-iommu doesn't allow UNMAP to split a mapping created
  302. * with a single MAP request, so remove the full mapping.
  303. */
  304. unmapped += mapping->iova.last - mapping->iova.start + 1;
  305. interval_tree_remove(node, &vdomain->mappings);
  306. kfree(mapping);
  307. }
  308. spin_unlock_irqrestore(&vdomain->mappings_lock, flags);
  309. return unmapped;
  310. }
  311. /*
  312. * Fill the domain with identity mappings, skipping the device's reserved
  313. * regions.
  314. */
  315. static int viommu_domain_map_identity(struct viommu_endpoint *vdev,
  316. struct viommu_domain *vdomain)
  317. {
  318. int ret;
  319. struct iommu_resv_region *resv;
  320. u64 iova = vdomain->domain.geometry.aperture_start;
  321. u64 limit = vdomain->domain.geometry.aperture_end;
  322. u32 flags = VIRTIO_IOMMU_MAP_F_READ | VIRTIO_IOMMU_MAP_F_WRITE;
  323. unsigned long granule = 1UL << __ffs(vdomain->domain.pgsize_bitmap);
  324. iova = ALIGN(iova, granule);
  325. limit = ALIGN_DOWN(limit + 1, granule) - 1;
  326. list_for_each_entry(resv, &vdev->resv_regions, list) {
  327. u64 resv_start = ALIGN_DOWN(resv->start, granule);
  328. u64 resv_end = ALIGN(resv->start + resv->length, granule) - 1;
  329. if (resv_end < iova || resv_start > limit)
  330. /* No overlap */
  331. continue;
  332. if (resv_start > iova) {
  333. ret = viommu_add_mapping(vdomain, iova, resv_start - 1,
  334. (phys_addr_t)iova, flags);
  335. if (ret)
  336. goto err_unmap;
  337. }
  338. if (resv_end >= limit)
  339. return 0;
  340. iova = resv_end + 1;
  341. }
  342. ret = viommu_add_mapping(vdomain, iova, limit, (phys_addr_t)iova,
  343. flags);
  344. if (ret)
  345. goto err_unmap;
  346. return 0;
  347. err_unmap:
  348. viommu_del_mappings(vdomain, 0, iova);
  349. return ret;
  350. }
  351. /*
  352. * viommu_replay_mappings - re-send MAP requests
  353. *
  354. * When reattaching a domain that was previously detached from all endpoints,
  355. * mappings were deleted from the device. Re-create the mappings available in
  356. * the internal tree.
  357. */
  358. static int viommu_replay_mappings(struct viommu_domain *vdomain)
  359. {
  360. int ret = 0;
  361. unsigned long flags;
  362. struct viommu_mapping *mapping;
  363. struct interval_tree_node *node;
  364. struct virtio_iommu_req_map map;
  365. spin_lock_irqsave(&vdomain->mappings_lock, flags);
  366. node = interval_tree_iter_first(&vdomain->mappings, 0, -1UL);
  367. while (node) {
  368. mapping = container_of(node, struct viommu_mapping, iova);
  369. map = (struct virtio_iommu_req_map) {
  370. .head.type = VIRTIO_IOMMU_T_MAP,
  371. .domain = cpu_to_le32(vdomain->id),
  372. .virt_start = cpu_to_le64(mapping->iova.start),
  373. .virt_end = cpu_to_le64(mapping->iova.last),
  374. .phys_start = cpu_to_le64(mapping->paddr),
  375. .flags = cpu_to_le32(mapping->flags),
  376. };
  377. ret = viommu_send_req_sync(vdomain->viommu, &map, sizeof(map));
  378. if (ret)
  379. break;
  380. node = interval_tree_iter_next(node, 0, -1UL);
  381. }
  382. spin_unlock_irqrestore(&vdomain->mappings_lock, flags);
  383. return ret;
  384. }
  385. static int viommu_add_resv_mem(struct viommu_endpoint *vdev,
  386. struct virtio_iommu_probe_resv_mem *mem,
  387. size_t len)
  388. {
  389. size_t size;
  390. u64 start64, end64;
  391. phys_addr_t start, end;
  392. struct iommu_resv_region *region = NULL, *next;
  393. unsigned long prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
  394. start = start64 = le64_to_cpu(mem->start);
  395. end = end64 = le64_to_cpu(mem->end);
  396. size = end64 - start64 + 1;
  397. /* Catch any overflow, including the unlikely end64 - start64 + 1 = 0 */
  398. if (start != start64 || end != end64 || size < end64 - start64)
  399. return -EOVERFLOW;
  400. if (len < sizeof(*mem))
  401. return -EINVAL;
  402. switch (mem->subtype) {
  403. default:
  404. dev_warn(vdev->dev, "unknown resv mem subtype 0x%x\n",
  405. mem->subtype);
  406. fallthrough;
  407. case VIRTIO_IOMMU_RESV_MEM_T_RESERVED:
  408. region = iommu_alloc_resv_region(start, size, 0,
  409. IOMMU_RESV_RESERVED,
  410. GFP_KERNEL);
  411. break;
  412. case VIRTIO_IOMMU_RESV_MEM_T_MSI:
  413. region = iommu_alloc_resv_region(start, size, prot,
  414. IOMMU_RESV_MSI,
  415. GFP_KERNEL);
  416. break;
  417. }
  418. if (!region)
  419. return -ENOMEM;
  420. /* Keep the list sorted */
  421. list_for_each_entry(next, &vdev->resv_regions, list) {
  422. if (next->start > region->start)
  423. break;
  424. }
  425. list_add_tail(&region->list, &next->list);
  426. return 0;
  427. }
  428. static int viommu_probe_endpoint(struct viommu_dev *viommu, struct device *dev)
  429. {
  430. int ret;
  431. u16 type, len;
  432. size_t cur = 0;
  433. size_t probe_len;
  434. struct virtio_iommu_req_probe *probe;
  435. struct virtio_iommu_probe_property *prop;
  436. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  437. struct viommu_endpoint *vdev = dev_iommu_priv_get(dev);
  438. if (!fwspec->num_ids)
  439. return -EINVAL;
  440. probe_len = sizeof(*probe) + viommu->probe_size +
  441. sizeof(struct virtio_iommu_req_tail);
  442. probe = kzalloc(probe_len, GFP_KERNEL);
  443. if (!probe)
  444. return -ENOMEM;
  445. probe->head.type = VIRTIO_IOMMU_T_PROBE;
  446. /*
  447. * For now, assume that properties of an endpoint that outputs multiple
  448. * IDs are consistent. Only probe the first one.
  449. */
  450. probe->endpoint = cpu_to_le32(fwspec->ids[0]);
  451. ret = viommu_send_req_sync(viommu, probe, probe_len);
  452. if (ret)
  453. goto out_free;
  454. prop = (void *)probe->properties;
  455. type = le16_to_cpu(prop->type) & VIRTIO_IOMMU_PROBE_T_MASK;
  456. while (type != VIRTIO_IOMMU_PROBE_T_NONE &&
  457. cur < viommu->probe_size) {
  458. len = le16_to_cpu(prop->length) + sizeof(*prop);
  459. switch (type) {
  460. case VIRTIO_IOMMU_PROBE_T_RESV_MEM:
  461. ret = viommu_add_resv_mem(vdev, (void *)prop, len);
  462. break;
  463. default:
  464. dev_err(dev, "unknown viommu prop 0x%x\n", type);
  465. }
  466. if (ret)
  467. dev_err(dev, "failed to parse viommu prop 0x%x\n", type);
  468. cur += len;
  469. if (cur >= viommu->probe_size)
  470. break;
  471. prop = (void *)probe->properties + cur;
  472. type = le16_to_cpu(prop->type) & VIRTIO_IOMMU_PROBE_T_MASK;
  473. }
  474. out_free:
  475. kfree(probe);
  476. return ret;
  477. }
  478. static int viommu_fault_handler(struct viommu_dev *viommu,
  479. struct virtio_iommu_fault *fault)
  480. {
  481. char *reason_str;
  482. u8 reason = fault->reason;
  483. u32 flags = le32_to_cpu(fault->flags);
  484. u32 endpoint = le32_to_cpu(fault->endpoint);
  485. u64 address = le64_to_cpu(fault->address);
  486. switch (reason) {
  487. case VIRTIO_IOMMU_FAULT_R_DOMAIN:
  488. reason_str = "domain";
  489. break;
  490. case VIRTIO_IOMMU_FAULT_R_MAPPING:
  491. reason_str = "page";
  492. break;
  493. case VIRTIO_IOMMU_FAULT_R_UNKNOWN:
  494. default:
  495. reason_str = "unknown";
  496. break;
  497. }
  498. /* TODO: find EP by ID and report_iommu_fault */
  499. if (flags & VIRTIO_IOMMU_FAULT_F_ADDRESS)
  500. dev_err_ratelimited(viommu->dev, "%s fault from EP %u at %#llx [%s%s%s]\n",
  501. reason_str, endpoint, address,
  502. flags & VIRTIO_IOMMU_FAULT_F_READ ? "R" : "",
  503. flags & VIRTIO_IOMMU_FAULT_F_WRITE ? "W" : "",
  504. flags & VIRTIO_IOMMU_FAULT_F_EXEC ? "X" : "");
  505. else
  506. dev_err_ratelimited(viommu->dev, "%s fault from EP %u\n",
  507. reason_str, endpoint);
  508. return 0;
  509. }
  510. static void viommu_event_handler(struct virtqueue *vq)
  511. {
  512. int ret;
  513. unsigned int len;
  514. struct scatterlist sg[1];
  515. struct viommu_event *evt;
  516. struct viommu_dev *viommu = vq->vdev->priv;
  517. while ((evt = virtqueue_get_buf(vq, &len)) != NULL) {
  518. if (len > sizeof(*evt)) {
  519. dev_err(viommu->dev,
  520. "invalid event buffer (len %u != %zu)\n",
  521. len, sizeof(*evt));
  522. } else if (!(evt->head & VIOMMU_FAULT_RESV_MASK)) {
  523. viommu_fault_handler(viommu, &evt->fault);
  524. }
  525. sg_init_one(sg, evt, sizeof(*evt));
  526. ret = virtqueue_add_inbuf(vq, sg, 1, evt, GFP_ATOMIC);
  527. if (ret)
  528. dev_err(viommu->dev, "could not add event buffer\n");
  529. }
  530. virtqueue_kick(vq);
  531. }
  532. /* IOMMU API */
  533. static struct iommu_domain *viommu_domain_alloc(unsigned type)
  534. {
  535. struct viommu_domain *vdomain;
  536. if (type != IOMMU_DOMAIN_UNMANAGED &&
  537. type != IOMMU_DOMAIN_DMA &&
  538. type != IOMMU_DOMAIN_IDENTITY)
  539. return NULL;
  540. vdomain = kzalloc(sizeof(*vdomain), GFP_KERNEL);
  541. if (!vdomain)
  542. return NULL;
  543. mutex_init(&vdomain->mutex);
  544. spin_lock_init(&vdomain->mappings_lock);
  545. vdomain->mappings = RB_ROOT_CACHED;
  546. return &vdomain->domain;
  547. }
  548. static int viommu_domain_finalise(struct viommu_endpoint *vdev,
  549. struct iommu_domain *domain)
  550. {
  551. int ret;
  552. unsigned long viommu_page_size;
  553. struct viommu_dev *viommu = vdev->viommu;
  554. struct viommu_domain *vdomain = to_viommu_domain(domain);
  555. viommu_page_size = 1UL << __ffs(viommu->pgsize_bitmap);
  556. if (viommu_page_size > PAGE_SIZE) {
  557. dev_err(vdev->dev,
  558. "granule 0x%lx larger than system page size 0x%lx\n",
  559. viommu_page_size, PAGE_SIZE);
  560. return -EINVAL;
  561. }
  562. ret = ida_alloc_range(&viommu->domain_ids, viommu->first_domain,
  563. viommu->last_domain, GFP_KERNEL);
  564. if (ret < 0)
  565. return ret;
  566. vdomain->id = (unsigned int)ret;
  567. domain->pgsize_bitmap = viommu->pgsize_bitmap;
  568. domain->geometry = viommu->geometry;
  569. vdomain->map_flags = viommu->map_flags;
  570. vdomain->viommu = viommu;
  571. if (domain->type == IOMMU_DOMAIN_IDENTITY) {
  572. if (virtio_has_feature(viommu->vdev,
  573. VIRTIO_IOMMU_F_BYPASS_CONFIG)) {
  574. vdomain->bypass = true;
  575. return 0;
  576. }
  577. ret = viommu_domain_map_identity(vdev, vdomain);
  578. if (ret) {
  579. ida_free(&viommu->domain_ids, vdomain->id);
  580. vdomain->viommu = NULL;
  581. return -EOPNOTSUPP;
  582. }
  583. }
  584. return 0;
  585. }
  586. static void viommu_domain_free(struct iommu_domain *domain)
  587. {
  588. struct viommu_domain *vdomain = to_viommu_domain(domain);
  589. /* Free all remaining mappings */
  590. viommu_del_mappings(vdomain, 0, ULLONG_MAX);
  591. if (vdomain->viommu)
  592. ida_free(&vdomain->viommu->domain_ids, vdomain->id);
  593. kfree(vdomain);
  594. }
  595. static int viommu_attach_dev(struct iommu_domain *domain, struct device *dev)
  596. {
  597. int i;
  598. int ret = 0;
  599. struct virtio_iommu_req_attach req;
  600. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  601. struct viommu_endpoint *vdev = dev_iommu_priv_get(dev);
  602. struct viommu_domain *vdomain = to_viommu_domain(domain);
  603. mutex_lock(&vdomain->mutex);
  604. if (!vdomain->viommu) {
  605. /*
  606. * Properly initialize the domain now that we know which viommu
  607. * owns it.
  608. */
  609. ret = viommu_domain_finalise(vdev, domain);
  610. } else if (vdomain->viommu != vdev->viommu) {
  611. dev_err(dev, "cannot attach to foreign vIOMMU\n");
  612. ret = -EXDEV;
  613. }
  614. mutex_unlock(&vdomain->mutex);
  615. if (ret)
  616. return ret;
  617. /*
  618. * In the virtio-iommu device, when attaching the endpoint to a new
  619. * domain, it is detached from the old one and, if as a result the
  620. * old domain isn't attached to any endpoint, all mappings are removed
  621. * from the old domain and it is freed.
  622. *
  623. * In the driver the old domain still exists, and its mappings will be
  624. * recreated if it gets reattached to an endpoint. Otherwise it will be
  625. * freed explicitly.
  626. *
  627. * vdev->vdomain is protected by group->mutex
  628. */
  629. if (vdev->vdomain)
  630. vdev->vdomain->nr_endpoints--;
  631. req = (struct virtio_iommu_req_attach) {
  632. .head.type = VIRTIO_IOMMU_T_ATTACH,
  633. .domain = cpu_to_le32(vdomain->id),
  634. };
  635. if (vdomain->bypass)
  636. req.flags |= cpu_to_le32(VIRTIO_IOMMU_ATTACH_F_BYPASS);
  637. for (i = 0; i < fwspec->num_ids; i++) {
  638. req.endpoint = cpu_to_le32(fwspec->ids[i]);
  639. ret = viommu_send_req_sync(vdomain->viommu, &req, sizeof(req));
  640. if (ret)
  641. return ret;
  642. }
  643. if (!vdomain->nr_endpoints) {
  644. /*
  645. * This endpoint is the first to be attached to the domain.
  646. * Replay existing mappings (e.g. SW MSI).
  647. */
  648. ret = viommu_replay_mappings(vdomain);
  649. if (ret)
  650. return ret;
  651. }
  652. vdomain->nr_endpoints++;
  653. vdev->vdomain = vdomain;
  654. return 0;
  655. }
  656. static void viommu_detach_dev(struct viommu_endpoint *vdev)
  657. {
  658. int i;
  659. struct virtio_iommu_req_detach req;
  660. struct viommu_domain *vdomain = vdev->vdomain;
  661. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(vdev->dev);
  662. if (!vdomain)
  663. return;
  664. req = (struct virtio_iommu_req_detach) {
  665. .head.type = VIRTIO_IOMMU_T_DETACH,
  666. .domain = cpu_to_le32(vdomain->id),
  667. };
  668. for (i = 0; i < fwspec->num_ids; i++) {
  669. req.endpoint = cpu_to_le32(fwspec->ids[i]);
  670. WARN_ON(viommu_send_req_sync(vdev->viommu, &req, sizeof(req)));
  671. }
  672. vdomain->nr_endpoints--;
  673. vdev->vdomain = NULL;
  674. }
  675. static int viommu_map_pages(struct iommu_domain *domain, unsigned long iova,
  676. phys_addr_t paddr, size_t pgsize, size_t pgcount,
  677. int prot, gfp_t gfp, size_t *mapped)
  678. {
  679. int ret;
  680. u32 flags;
  681. size_t size = pgsize * pgcount;
  682. u64 end = iova + size - 1;
  683. struct virtio_iommu_req_map map;
  684. struct viommu_domain *vdomain = to_viommu_domain(domain);
  685. flags = (prot & IOMMU_READ ? VIRTIO_IOMMU_MAP_F_READ : 0) |
  686. (prot & IOMMU_WRITE ? VIRTIO_IOMMU_MAP_F_WRITE : 0) |
  687. (prot & IOMMU_MMIO ? VIRTIO_IOMMU_MAP_F_MMIO : 0);
  688. if (flags & ~vdomain->map_flags)
  689. return -EINVAL;
  690. ret = viommu_add_mapping(vdomain, iova, end, paddr, flags);
  691. if (ret)
  692. return ret;
  693. if (vdomain->nr_endpoints) {
  694. map = (struct virtio_iommu_req_map) {
  695. .head.type = VIRTIO_IOMMU_T_MAP,
  696. .domain = cpu_to_le32(vdomain->id),
  697. .virt_start = cpu_to_le64(iova),
  698. .phys_start = cpu_to_le64(paddr),
  699. .virt_end = cpu_to_le64(end),
  700. .flags = cpu_to_le32(flags),
  701. };
  702. ret = viommu_send_req_sync(vdomain->viommu, &map, sizeof(map));
  703. if (ret) {
  704. viommu_del_mappings(vdomain, iova, end);
  705. return ret;
  706. }
  707. }
  708. if (mapped)
  709. *mapped = size;
  710. return 0;
  711. }
  712. static size_t viommu_unmap_pages(struct iommu_domain *domain, unsigned long iova,
  713. size_t pgsize, size_t pgcount,
  714. struct iommu_iotlb_gather *gather)
  715. {
  716. int ret = 0;
  717. size_t unmapped;
  718. struct virtio_iommu_req_unmap unmap;
  719. struct viommu_domain *vdomain = to_viommu_domain(domain);
  720. size_t size = pgsize * pgcount;
  721. unmapped = viommu_del_mappings(vdomain, iova, iova + size - 1);
  722. if (unmapped < size)
  723. return 0;
  724. /* Device already removed all mappings after detach. */
  725. if (!vdomain->nr_endpoints)
  726. return unmapped;
  727. unmap = (struct virtio_iommu_req_unmap) {
  728. .head.type = VIRTIO_IOMMU_T_UNMAP,
  729. .domain = cpu_to_le32(vdomain->id),
  730. .virt_start = cpu_to_le64(iova),
  731. .virt_end = cpu_to_le64(iova + unmapped - 1),
  732. };
  733. ret = viommu_add_req(vdomain->viommu, &unmap, sizeof(unmap));
  734. return ret ? 0 : unmapped;
  735. }
  736. static phys_addr_t viommu_iova_to_phys(struct iommu_domain *domain,
  737. dma_addr_t iova)
  738. {
  739. u64 paddr = 0;
  740. unsigned long flags;
  741. struct viommu_mapping *mapping;
  742. struct interval_tree_node *node;
  743. struct viommu_domain *vdomain = to_viommu_domain(domain);
  744. spin_lock_irqsave(&vdomain->mappings_lock, flags);
  745. node = interval_tree_iter_first(&vdomain->mappings, iova, iova);
  746. if (node) {
  747. mapping = container_of(node, struct viommu_mapping, iova);
  748. paddr = mapping->paddr + (iova - mapping->iova.start);
  749. }
  750. spin_unlock_irqrestore(&vdomain->mappings_lock, flags);
  751. return paddr;
  752. }
  753. static void viommu_iotlb_sync(struct iommu_domain *domain,
  754. struct iommu_iotlb_gather *gather)
  755. {
  756. struct viommu_domain *vdomain = to_viommu_domain(domain);
  757. viommu_sync_req(vdomain->viommu);
  758. }
  759. static void viommu_get_resv_regions(struct device *dev, struct list_head *head)
  760. {
  761. struct iommu_resv_region *entry, *new_entry, *msi = NULL;
  762. struct viommu_endpoint *vdev = dev_iommu_priv_get(dev);
  763. int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
  764. list_for_each_entry(entry, &vdev->resv_regions, list) {
  765. if (entry->type == IOMMU_RESV_MSI)
  766. msi = entry;
  767. new_entry = kmemdup(entry, sizeof(*entry), GFP_KERNEL);
  768. if (!new_entry)
  769. return;
  770. list_add_tail(&new_entry->list, head);
  771. }
  772. /*
  773. * If the device didn't register any bypass MSI window, add a
  774. * software-mapped region.
  775. */
  776. if (!msi) {
  777. msi = iommu_alloc_resv_region(MSI_IOVA_BASE, MSI_IOVA_LENGTH,
  778. prot, IOMMU_RESV_SW_MSI,
  779. GFP_KERNEL);
  780. if (!msi)
  781. return;
  782. list_add_tail(&msi->list, head);
  783. }
  784. iommu_dma_get_resv_regions(dev, head);
  785. }
  786. static struct iommu_ops viommu_ops;
  787. static struct virtio_driver virtio_iommu_drv;
  788. static int viommu_match_node(struct device *dev, const void *data)
  789. {
  790. return device_match_fwnode(dev->parent, data);
  791. }
  792. static struct viommu_dev *viommu_get_by_fwnode(struct fwnode_handle *fwnode)
  793. {
  794. struct device *dev = driver_find_device(&virtio_iommu_drv.driver, NULL,
  795. fwnode, viommu_match_node);
  796. put_device(dev);
  797. return dev ? dev_to_virtio(dev)->priv : NULL;
  798. }
  799. static struct iommu_device *viommu_probe_device(struct device *dev)
  800. {
  801. int ret;
  802. struct viommu_endpoint *vdev;
  803. struct viommu_dev *viommu = NULL;
  804. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  805. if (!fwspec || fwspec->ops != &viommu_ops)
  806. return ERR_PTR(-ENODEV);
  807. viommu = viommu_get_by_fwnode(fwspec->iommu_fwnode);
  808. if (!viommu)
  809. return ERR_PTR(-ENODEV);
  810. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  811. if (!vdev)
  812. return ERR_PTR(-ENOMEM);
  813. vdev->dev = dev;
  814. vdev->viommu = viommu;
  815. INIT_LIST_HEAD(&vdev->resv_regions);
  816. dev_iommu_priv_set(dev, vdev);
  817. if (viommu->probe_size) {
  818. /* Get additional information for this endpoint */
  819. ret = viommu_probe_endpoint(viommu, dev);
  820. if (ret)
  821. goto err_free_dev;
  822. }
  823. return &viommu->iommu;
  824. err_free_dev:
  825. iommu_put_resv_regions(dev, &vdev->resv_regions);
  826. kfree(vdev);
  827. return ERR_PTR(ret);
  828. }
  829. static void viommu_probe_finalize(struct device *dev)
  830. {
  831. #ifndef CONFIG_ARCH_HAS_SETUP_DMA_OPS
  832. /* First clear the DMA ops in case we're switching from a DMA domain */
  833. set_dma_ops(dev, NULL);
  834. iommu_setup_dma_ops(dev, 0, U64_MAX);
  835. #endif
  836. }
  837. static void viommu_release_device(struct device *dev)
  838. {
  839. struct viommu_endpoint *vdev = dev_iommu_priv_get(dev);
  840. viommu_detach_dev(vdev);
  841. iommu_put_resv_regions(dev, &vdev->resv_regions);
  842. kfree(vdev);
  843. }
  844. static struct iommu_group *viommu_device_group(struct device *dev)
  845. {
  846. if (dev_is_pci(dev))
  847. return pci_device_group(dev);
  848. else
  849. return generic_device_group(dev);
  850. }
  851. static int viommu_of_xlate(struct device *dev, struct of_phandle_args *args)
  852. {
  853. return iommu_fwspec_add_ids(dev, args->args, 1);
  854. }
  855. static bool viommu_capable(struct device *dev, enum iommu_cap cap)
  856. {
  857. switch (cap) {
  858. case IOMMU_CAP_CACHE_COHERENCY:
  859. return true;
  860. default:
  861. return false;
  862. }
  863. }
  864. static struct iommu_ops viommu_ops = {
  865. .capable = viommu_capable,
  866. .domain_alloc = viommu_domain_alloc,
  867. .probe_device = viommu_probe_device,
  868. .probe_finalize = viommu_probe_finalize,
  869. .release_device = viommu_release_device,
  870. .device_group = viommu_device_group,
  871. .get_resv_regions = viommu_get_resv_regions,
  872. .of_xlate = viommu_of_xlate,
  873. .owner = THIS_MODULE,
  874. .default_domain_ops = &(const struct iommu_domain_ops) {
  875. .attach_dev = viommu_attach_dev,
  876. .map_pages = viommu_map_pages,
  877. .unmap_pages = viommu_unmap_pages,
  878. .iova_to_phys = viommu_iova_to_phys,
  879. .iotlb_sync = viommu_iotlb_sync,
  880. .free = viommu_domain_free,
  881. }
  882. };
  883. static int viommu_init_vqs(struct viommu_dev *viommu)
  884. {
  885. struct virtio_device *vdev = dev_to_virtio(viommu->dev);
  886. const char *names[] = { "request", "event" };
  887. vq_callback_t *callbacks[] = {
  888. NULL, /* No async requests */
  889. viommu_event_handler,
  890. };
  891. return virtio_find_vqs(vdev, VIOMMU_NR_VQS, viommu->vqs, callbacks,
  892. names, NULL);
  893. }
  894. static int viommu_fill_evtq(struct viommu_dev *viommu)
  895. {
  896. int i, ret;
  897. struct scatterlist sg[1];
  898. struct viommu_event *evts;
  899. struct virtqueue *vq = viommu->vqs[VIOMMU_EVENT_VQ];
  900. size_t nr_evts = vq->num_free;
  901. viommu->evts = evts = devm_kmalloc_array(viommu->dev, nr_evts,
  902. sizeof(*evts), GFP_KERNEL);
  903. if (!evts)
  904. return -ENOMEM;
  905. for (i = 0; i < nr_evts; i++) {
  906. sg_init_one(sg, &evts[i], sizeof(*evts));
  907. ret = virtqueue_add_inbuf(vq, sg, 1, &evts[i], GFP_KERNEL);
  908. if (ret)
  909. return ret;
  910. }
  911. return 0;
  912. }
  913. static int viommu_probe(struct virtio_device *vdev)
  914. {
  915. struct device *parent_dev = vdev->dev.parent;
  916. struct viommu_dev *viommu = NULL;
  917. struct device *dev = &vdev->dev;
  918. u64 input_start = 0;
  919. u64 input_end = -1UL;
  920. int ret;
  921. if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
  922. !virtio_has_feature(vdev, VIRTIO_IOMMU_F_MAP_UNMAP))
  923. return -ENODEV;
  924. viommu = devm_kzalloc(dev, sizeof(*viommu), GFP_KERNEL);
  925. if (!viommu)
  926. return -ENOMEM;
  927. spin_lock_init(&viommu->request_lock);
  928. ida_init(&viommu->domain_ids);
  929. viommu->dev = dev;
  930. viommu->vdev = vdev;
  931. INIT_LIST_HEAD(&viommu->requests);
  932. ret = viommu_init_vqs(viommu);
  933. if (ret)
  934. return ret;
  935. virtio_cread_le(vdev, struct virtio_iommu_config, page_size_mask,
  936. &viommu->pgsize_bitmap);
  937. if (!viommu->pgsize_bitmap) {
  938. ret = -EINVAL;
  939. goto err_free_vqs;
  940. }
  941. viommu->map_flags = VIRTIO_IOMMU_MAP_F_READ | VIRTIO_IOMMU_MAP_F_WRITE;
  942. viommu->last_domain = ~0U;
  943. /* Optional features */
  944. virtio_cread_le_feature(vdev, VIRTIO_IOMMU_F_INPUT_RANGE,
  945. struct virtio_iommu_config, input_range.start,
  946. &input_start);
  947. virtio_cread_le_feature(vdev, VIRTIO_IOMMU_F_INPUT_RANGE,
  948. struct virtio_iommu_config, input_range.end,
  949. &input_end);
  950. virtio_cread_le_feature(vdev, VIRTIO_IOMMU_F_DOMAIN_RANGE,
  951. struct virtio_iommu_config, domain_range.start,
  952. &viommu->first_domain);
  953. virtio_cread_le_feature(vdev, VIRTIO_IOMMU_F_DOMAIN_RANGE,
  954. struct virtio_iommu_config, domain_range.end,
  955. &viommu->last_domain);
  956. virtio_cread_le_feature(vdev, VIRTIO_IOMMU_F_PROBE,
  957. struct virtio_iommu_config, probe_size,
  958. &viommu->probe_size);
  959. viommu->geometry = (struct iommu_domain_geometry) {
  960. .aperture_start = input_start,
  961. .aperture_end = input_end,
  962. .force_aperture = true,
  963. };
  964. if (virtio_has_feature(vdev, VIRTIO_IOMMU_F_MMIO))
  965. viommu->map_flags |= VIRTIO_IOMMU_MAP_F_MMIO;
  966. viommu_ops.pgsize_bitmap = viommu->pgsize_bitmap;
  967. virtio_device_ready(vdev);
  968. /* Populate the event queue with buffers */
  969. ret = viommu_fill_evtq(viommu);
  970. if (ret)
  971. goto err_free_vqs;
  972. ret = iommu_device_sysfs_add(&viommu->iommu, dev, NULL, "%s",
  973. virtio_bus_name(vdev));
  974. if (ret)
  975. goto err_free_vqs;
  976. iommu_device_register(&viommu->iommu, &viommu_ops, parent_dev);
  977. vdev->priv = viommu;
  978. dev_info(dev, "input address: %u bits\n",
  979. order_base_2(viommu->geometry.aperture_end));
  980. dev_info(dev, "page mask: %#llx\n", viommu->pgsize_bitmap);
  981. return 0;
  982. err_free_vqs:
  983. vdev->config->del_vqs(vdev);
  984. return ret;
  985. }
  986. static void viommu_remove(struct virtio_device *vdev)
  987. {
  988. struct viommu_dev *viommu = vdev->priv;
  989. iommu_device_sysfs_remove(&viommu->iommu);
  990. iommu_device_unregister(&viommu->iommu);
  991. /* Stop all virtqueues */
  992. virtio_reset_device(vdev);
  993. vdev->config->del_vqs(vdev);
  994. dev_info(&vdev->dev, "device removed\n");
  995. }
  996. static void viommu_config_changed(struct virtio_device *vdev)
  997. {
  998. dev_warn(&vdev->dev, "config changed\n");
  999. }
  1000. static unsigned int features[] = {
  1001. VIRTIO_IOMMU_F_MAP_UNMAP,
  1002. VIRTIO_IOMMU_F_INPUT_RANGE,
  1003. VIRTIO_IOMMU_F_DOMAIN_RANGE,
  1004. VIRTIO_IOMMU_F_PROBE,
  1005. VIRTIO_IOMMU_F_MMIO,
  1006. VIRTIO_IOMMU_F_BYPASS_CONFIG,
  1007. };
  1008. static struct virtio_device_id id_table[] = {
  1009. { VIRTIO_ID_IOMMU, VIRTIO_DEV_ANY_ID },
  1010. { 0 },
  1011. };
  1012. MODULE_DEVICE_TABLE(virtio, id_table);
  1013. static struct virtio_driver virtio_iommu_drv = {
  1014. .driver.name = KBUILD_MODNAME,
  1015. .driver.owner = THIS_MODULE,
  1016. .id_table = id_table,
  1017. .feature_table = features,
  1018. .feature_table_size = ARRAY_SIZE(features),
  1019. .probe = viommu_probe,
  1020. .remove = viommu_remove,
  1021. .config_changed = viommu_config_changed,
  1022. };
  1023. module_virtio_driver(virtio_iommu_drv);
  1024. MODULE_DESCRIPTION("Virtio IOMMU driver");
  1025. MODULE_AUTHOR("Jean-Philippe Brucker <[email protected]>");
  1026. MODULE_LICENSE("GPL v2");